Java output to file

Danimal1209

Senior member
Nov 9, 2011
355
0
0
So, I am working on something where I need to output all the Book objects in an arraylist to an output file. Here is my code:

FileWriter fileWriter = new FileWriter(fileName + ".txt");
BufferedWriter writeBuffer = new BufferedWriter(fileWriter);
for(int i = 0; i < libraryBooks.size(); i++)
{
writeBuffer.write(libraryBooks.get(i).toString() + "\n");
writeBuffer.write("\n");
}
writeBuffer.close();
fileWriter.close();

With this code, all the objects are written, but the new line command has no effect. I need to know how to get each book to print on a new line. Any help is appreciated.
 

BrightCandle

Diamond Member
Mar 15, 2007
4,762
0
76
Bare in mind that you have a resource leak problem there as well, you need those .close() calls in a finally block. Follow the IO pattern and you'll be fine.
 

cytg111

Lifer
Mar 17, 2008
25,599
15,123
136
you're basicly up against System.getProperty("line.separator") ... different platforms different ... platforms.