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
-
I have a longer response for this, but as for your specific question, are you getting any exceptions, etc?Michael Minella– Michael Minella2018年05月11日 15:31:48 +00:00Commented May 11, 2018 at 15:31
-
1Not sure if this will fix your problem, but give it a try: use writer.flush(); after writer.printlncisk– cisk2018年05月11日 15:34:20 +00:00Commented May 11, 2018 at 15:34
1 Answer 1
A couple things here:
- Why not use the
FlatFileItemWriter? I don't see anything you're doing here that can't be done with it. - Don't use
Closablefor anItemWriter. Spring Batch has a lifecycle for opening and closing resources. It's theItemStreaminterface. You can read more about it in the documentation here: https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/ItemStream.html - Your
PrintWriteris 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 callwriter.flush()at the end of the write method.
answered May 11, 2018 at 16:44
Michael Minella
21.6k4 gold badges61 silver badges69 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-java