• Post Reply Bookmark Topic Watch Topic
  • New Topic

Enter in text file  RSS feed

 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

I'm entering text in a text file. I need to put an "enter" after each line. I used fileWriter.write("my message" + "\n");

This doesn't work.

Any rescue?
Tx in advance
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kejal

If you are working in windows then most programs expect to see ascii cr + lf. I think you achieve that with "\r\n". Hope that helps.

Ed
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cas use PrintWriter to format the output. It allows to write new line with println(). BufferedWriter can also write new line with newLine().
Or write platform specific line separator by
 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eddie/Vlado!

Thanks for your replies. I've tried "\r\f" and it works fine. But am afraid if it'll work on UNIX cos tat's where my application's gonna go.

As for System.getProperties("line.separator"), may i confirm this on the Java site?

Tx
kej
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When in doubt, read the API documentation. There's a list of properties under the getProperties() method. It is probably easier to do as Vlado suggested and use either PrintWriter or BufferedWriter (BufferedWriter also enhances IO performance).
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrap your FileWriter in a PrintWriter - that's what it's for. Here's how:

That's completely platform independent. Notice the similarity to the familiar System.out.println(), which is also a PrintWriter. As Joe says, if in doubt, read the manual (API docs).

Jules
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!