I implemented an OutputWriterService.class
to write content in the same file multiple times.
The writeToCsvFile
method is called round about 50.000 times and includes a list of descrptions which also needs to iterate and write to the file.
Is it a good way to instantiate a new Filewriter
, BufferedWriter
and PrintWriter
for every call?
public void writeToCsvFile(List<CustomTypeDescription> descriptions){
try(FileWriter fw = new FileWriter(FILE_NAME, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw))
{
for(CustomTypeDescription description : descriptions){
out.println(description.toString());
}
//here the fw.flush()?
} catch (IOException e) {
e.printStackTrace();
}
}
public void createOutputFile() throws IOException {
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(FILE_NAME), "utf-8"))) {
}
}
Update:
Is it necessary to flush File
-, Buffered
- and Print
writer one by one or just the FileWriter
. I would do it after the loop?