I am using Writer class in Java for UTF8 encoding output. How do I insert new line when writing?

StackOverflow https://stackoverflow.com/questions/21659079

  •  09-10-2022
  •  | 
  •  

Pregunta

My snippet code are as follow:

        FileOutputStream fos = new FileOutputStream("newFile.txt"); 
        OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8");
        Writer w = out;

        for(String str : arrayName) 
        {
            w.write("sample");
            //I wish to insert new line here
        }

        w.close();

Is there other way than to write a blank space manually? i.e. w.write("\n");

EDITED. Sorry for causing some misunderstanding regarding if I am looking for new line or blank space.

¿Fue útil?

Solución

For platform independent way use this:

w.write(System.getProperty("line.separator"));

Otros consejos

w.write("\n");

Well apparently this answer was to short.

Read Java documentation about special characters For blank space (space character) just do:

w.write(" ");

try this

PrintWriter bw = new PrintWriter(new File("newFile.txt"), "UTF-8");
...
bw.println("sample");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top