1

at the End of the Batch File Created with the Name ReportDetail, But nothing is there in this file

public class ReportWriter implements ItemWriter<Record>,Closeable {
 private PrintWriter writer = null;
 public ReportWriter() {
 OutputStream out;
 try {
 out = new FileOutputStream("ReportDetail.txt");
 } catch (FileNotFoundException e) {
 System.out.println("Exception e" + e);
 out = System.out;
 }
 try {
 this.writer = new PrintWriter(out);
 }catch (Exception e) {
 e.printStackTrace();
 System.out.println(""+e);
 }
 }
 @Override
 public void write(final List<? extends Record> items) throws Exception {
 for (Record item : items) {
 System.out.println("item.getCode()"); // this Prints the Code
 writer.println(item.getCode()); // Not Working
 }
 }
 @PreDestroy
 @Override
 public void close() throws IOException {
 writer.close();
 }
}
Michael Minella
21.6k4 gold badges61 silver badges69 bronze badges
asked May 11, 2018 at 9:07
2
  • I have a longer response for this, but as for your specific question, are you getting any exceptions, etc? Commented May 11, 2018 at 15:31
  • 1
    Not sure if this will fix your problem, but give it a try: use writer.flush(); after writer.println Commented May 11, 2018 at 15:34

1 Answer 1

2

A couple things here:

  1. Why not use the FlatFileItemWriter? I don't see anything you're doing here that can't be done with it.
  2. Don't use Closable for an ItemWriter. Spring Batch has a lifecycle for opening and closing resources. It's the ItemStream interface. You can read more about it in the documentation here: https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/ItemStream.html
  3. Your PrintWriter is not configured to flush automatically (it's false by default). You need to either enable that (so that your writer flushes after each line) or explicitly call writer.flush() at the end of the write method.
answered May 11, 2018 at 16:44
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.