Writing to a .txt File
I am new to Java and I had a question for a program I am writing. I want to save some data to a .txt file, and I got everything working other than one thing. I have no idea how to skip lines in the .txt file I tried /n as my friend suggested, but that didnt work. How should I go about doing this.
As the line separator is different on different OS's
Where do I put that in the code. I have:
String text = "Text" + variable
and then I need to make another on the .txt file to put more text.variables. Where would your code fit in?
EDIT: I'm using Windows XP
In your example it would be:
...or if your using it a lot do something like...
String text = "Text" + System.getProperty("line.separator");
private final String NL = System.getProperty("line.separator");
String text = "Text" + NL +
"MoreText" + NL;
Thank you, that helps alot!
EDIT: I tried your code out, using:
private final String newLine = System.getProperty("line.separator");
But when I compiled my code, I got an error message: "illegal start of expression" and it pointed to the line that I put your code at.
you can also just use \n\r
Different platforms will ignore the code that doesn't work. Unix/OS X use \n, windows \r generally.
Fix the error
You may not want the private... try this instead
Originally Posted by ngc0202
final String newLine = System.getProperty("line.separator");
Sorry did not mention that, I usually put stuff like that as a class level constant for use by any of the methods. Just remove "private" as the preivious poster mentioned. :)