Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Line break for fileOutputStream

 
Cathleen Meneses
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

given the ff code:
FileOutputStream streamOut = new FileOutputStream(new File(strPath),true);
byte [] b = new byte[content.length];
b= content.getBytes();
streamOut.write (b);
streamOut.close();

how can we insert line breaks after we write array b to the file?
thanks!! ^_^

 
Joe Ess
Bartender
Pie
Posts: 9265
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OutputStreams are usually used for binary files. They don't provide any methods to add line breaks so you have to get the line break sequence using System.getProperty("line.separator") and append it to your lines or write it after each line. Error prone and tedious.
A better alternative for working with character based files are the Reader classes. Several of those classes have methods to add new lines. For example, java.io.BufferedWriter has a newLine() method and java.io.PrintWriter has several println() methods.
 
Cathleen Meneses
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Joe! it really helped a lot! now i can move on ^_^ thanks again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic