Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write new line in Java FileOutputStream

I want to write a new line using a FileOutputStream; I have tried the following approaches, but none of them are working:

encfileout.write('\n');
encfileout.write("\n".getbytes());
encfileout.write(System.getProperty("line.separator").getBytes());
like image 873
Pavan Patidar Avatar asked Jun 16 '14 12:06

Pavan Patidar


1 Answers

This should work. Probably you forgot to call encfileout.flush().

However this is not the preferred way to write texts. You should wrap your output stream with PrintWriter and enjoy its println() methods:

PrintWriter writer = new PrintWriter(new OutputStreamWriter(encfileout, charset));

Alternatively you can use FileWriter instead of FileOutputStream from the beginning:

FileWriter fw = new FileWriter("myfile");
PrintWriter writer = new PrintWriter(fw);

Now just call

writer.println();

And do not forget to call flush() and close() when you finish your job.

like image 118
AlexR Avatar answered Sep 19 '22 17:09

AlexR