Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

appending to new line in text file

807597Oct 31 2005 — edited Aug 29 2009
I am trying to append text to a new line to a file. I set the append parameter to true and I try to output the new line character after I write the string to the file. However it is still appending the next string to the end of the first rather than in the line below. My code is below. Can someone please help me?

Thanks

public static boolean WriteToFile(String nameOfFile,
String contents)
{
FileWriter myFileWriter = null;

try
{
System.out.println("Attempting to write text to = " + nameOfFile);
myFileWriter = new FileWriter(nameOfFile,true);
myFileWriter.write(contents);
myFileWriter.write('\n');
myFileWriter.close();
return true;
}

catch (Exception e)
{
System.out.println("EXCEPTION thrown ");
return false;
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 26 2009
Added on Oct 31 2005
24 comments
3,616 views