How to write new line in Java FileOutputStream
Asked Answered
H

5

21

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());
Hakim answered 16/6, 2014 at 12:5 Comment(8)
Define "not working".Reveal
its not writing new lineHakim
@PavanPatidar: It is writing a newline. For sure.Sough
Try executing encfileout.write("Fant".getbytes()); encfileout.write("\n".getbytes()); encfileout.write("astic".getbytes()); If you see the whole word on a single line your problem is confirmed. Especially, the encfileout.write(System.getProperty("line.separator").getbytes()); should work.Palla
@Palla it is working on localhost but when i am deploying in on my server it does't works.Hakim
It could be a viewer problem... Try opening the file in EditPlus or Notepad++. Windows Notepad may not recognise line feed of another operating system. In which program are you viewing the file now?Palla
@Teddy:Thanks, you are right its viewing problem, notepad++ is showing correctly my data. Thanks again for bring my attention.Hakim
@PavanPatidar I added the last comment as an answer... you can accept if it solved your problem. If you accept it, this question will be marked as answered.Palla
P
10

It could be a viewer problem... Try opening the file in EditPlus or Notepad++. Windows Notepad may not recognize the line feed of another operating system. In which program are you viewing the file now?

Palla answered 16/6, 2014 at 20:20 Comment(0)
B
20

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.

Backrest answered 16/6, 2014 at 12:12 Comment(3)
I hope you don't mind that i added the missing charset parameter.Reveal
@Pavan Could you explain why it's not what you're looking for so the answer can be amended/another answer provided?Plain
FileWriter and PrintWriter are feature rich. The println() of Printwriter works great!!!Onagraceous
P
10

It could be a viewer problem... Try opening the file in EditPlus or Notepad++. Windows Notepad may not recognize the line feed of another operating system. In which program are you viewing the file now?

Palla answered 16/6, 2014 at 20:20 Comment(0)
F
4

To add a line break use

fileOutputStream.write(10);

here decimal value 10 represents newline in ASCII

Fowl answered 3/8, 2020 at 12:8 Comment(0)
R
4
String lineSeparator = System.getProperty("line.separator");
<br>
fos.write(lineSeparator.getBytes());
Robins answered 6/10, 2020 at 16:45 Comment(1)
Please consider adding some form of explanation with your answer. Code only answers are typically discouraged.Kresic
L
0

You can use Fos.write(System.lineSeparator().getBytes()). it worked for me.

Lydon answered 29/6, 2023 at 5:27 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Aggress

© 2022 - 2024 — McMap. All rights reserved.