Results 1 to 8 of 8
Thread: Writing to a .txt File
- 07-25-2009, 11:18 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 32
- Rep Power
- 0
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.
Last edited by ngc0202; 07-25-2009 at 11:22 PM.
- 07-26-2009, 12:28 AM #2
Member
- Join Date
- Dec 2008
- Posts
- 64
- Rep Power
- 0
Use:
As the line separator is different on different OS'sJava Code:System.getProperty("line.separator");
- 07-26-2009, 12:36 AM #3
Member
- Join Date
- Jul 2009
- Posts
- 32
- Rep Power
- 0
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
- 07-26-2009, 12:42 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 64
- Rep Power
- 0
In your example it would be:
...or if your using it a lot do something like...Java Code:String text = "Text" + System.getProperty("line.separator");
Java Code:private final String NL = System.getProperty("line.separator"); String text = "Text" + NL + "MoreText" + NL;
- 07-26-2009, 12:46 AM #5
Member
- Join Date
- Jul 2009
- Posts
- 32
- Rep Power
- 0
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.Last edited by ngc0202; 07-26-2009 at 12:52 AM.
- 07-26-2009, 02:51 AM #6
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.
- 07-26-2009, 02:55 AM #7
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Fix the error
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 07-26-2009, 03:18 AM #8
Member
- Join Date
- Dec 2008
- Posts
- 64
- Rep Power
- 0
Similar Threads
-
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM -
Writing to DAT or TXT file
By hunterbdb in forum Advanced JavaReplies: 7Last Post: 10-12-2008, 02:50 PM -
swapping the contents of the file and writing to another file
By Ms.Ranjan in forum New To JavaReplies: 9Last Post: 07-10-2008, 04:52 PM -
Writing to a file (at the end)
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:22 AM -
writing to a file
By bugger in forum New To JavaReplies: 1Last Post: 11-11-2007, 02:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks