Search code examples
javautf-8linewriter

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


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.


Solution

  • For platform independent way use this:

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