write newline into a file

219,289

Solution 1

To write text (rather than raw bytes) to a file you should consider using FileWriter. You should also wrap it in a BufferedWriter which will then give you the newLine method.

To write each word on a new line, use String.split to break your text into an array of words.

So here's a simple test of your requirement:

public static void main(String[] args) throws Exception {
    String nodeValue = "i am mostafa";

    // you want to output to file
    // BufferedWriter writer = new BufferedWriter(new FileWriter(file3, true));
    // but let's print to console while debugging
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    String[] words = nodeValue.split(" ");
    for (String word: words) {
        writer.write(word);
        writer.newLine();
    }
    writer.close();
}

The output is:

i
am
mostafa

Solution 2

Change the lines

if(nodeValue!=null)
    fop.write(nodeValue.getBytes());

fop.flush();

to

if(nodeValue!=null) {
    fop.write(nodeValue.getBytes());
    fop.write(System.getProperty("line.separator").getBytes());
}

fop.flush();

Update to address your edit:

In order to write each word on a different line, you need to split up your input string and then write each word separately.

private static void GetText(String nodeValue) throws IOException {

    if(!file3.exists()) {
        file3.createNewFile();
    }

    FileOutputStream fop=new FileOutputStream(file3,true);
    if(nodeValue!=null)
        for(final String s : nodeValue.split(" ")){
            fop.write(s.getBytes());
            fop.write(System.getProperty("line.separator").getBytes());
        }
    }

    fop.flush();
    fop.close();

}

Solution 3

Files.write(Paths.get(filepath),texttobewrittentofile,StandardOpenOption.APPEND ,StandardOpenOption.CREATE);    

This creates a file, if it does not exist If files exists, it is uses the existing file and text is appended If you want everyline to be written to the next line add lineSepartor for newline into file.

String texttobewrittentofile = text + System.lineSeparator();

Solution 4

if(!file3.exists()){
    file3.createNewFile();
}
FileOutputStream fop=new FileOutputStream(file3,true);
if(nodeValue!=null) fop.write(nodeValue.getBytes());

fop.write("\n".getBytes());

fop.flush();
fop.close();

You need to add a newline at the end of each write.

Solution 5

Solution:

in WINDOWS you should write "\r\n" for a new line.

Share:
219,289
lonesome
Author by

lonesome

Updated on March 18, 2020

Comments

  • lonesome
    lonesome over 4 years

    considering the following function

    private static void GetText(String nodeValue) throws IOException {
    
       if(!file3.exists()) {
           file3.createNewFile();
       }
    
       FileOutputStream fop=new FileOutputStream(file3,true);
       if(nodeValue!=null)
           fop.write(nodeValue.getBytes());
    
       fop.flush();
       fop.close();
    
    }
    

    what to add to make it to write each time in the next line?

    for example i want each words of a given string in a seperate lline for example:

    i am mostafa
    

    writes as:

     i
     am
     mostafa
    
  • Jack Edmonds
    Jack Edmonds over 12 years
    @Mostafa What program are you using to view the file? Also, are you using System.getProperty("line.separator")? (I edited my answer.)
  • Jack Edmonds
    Jack Edmonds over 12 years
    @Mostafa Are you writing "\n".getBytes() or System.getProperty("line.separator").getBytes()?
  • lonesome
    lonesome over 12 years
    with wordpad and office word still showing same answer as notpad shows
  • lonesome
    lonesome over 12 years
    but the point is i want it to write each byes of the string in a new line not each string ...did you get what i mean?
  • ewan.chalmers
    ewan.chalmers over 12 years
    You want each byte on a new line? If so, please make it clear in your question.
  • lonesome
    lonesome over 12 years
    i did it just now, sorry for that
  • lonesome
    lonesome over 12 years
    but it gives same result as my function in the question :( oh ~
  • lonesome
    lonesome over 12 years
    result is same, did you realized you declared s but actually didnt use it
  • ewan.chalmers
    ewan.chalmers over 12 years
    I think you must be missing something. See full example in updated answer.
  • Jack Edmonds
    Jack Edmonds over 12 years
    @Mostafa Oops. That was a mistake. I've fixed it.
  • lonesome
    lonesome over 12 years
    oh, now its working...on the debugging window but when i use to write into a file and them open it, its not like in the debugging window, why is that so?
  • lonesome
    lonesome over 12 years
    oh, now its working...on the debugging window but when i use to write into a file and them open it, its not like in the debugging window, why is that so?
  • ewan.chalmers
    ewan.chalmers over 12 years
    When I use the code above and write to file (use the commented writer), I see the exact same output as above in the file. Are you possibly executing Java on a Unix platform (so getting unix line feeds) and viewing the file with a Windows editor (like Notepad). That would explain why you don't see line feeds in the file.
  • lonesome
    lonesome over 12 years
    you may laugh but i think from beginning you were right! when i send it to the debugging window it shows them in new line but in the file they are followed in same line! why is it like this? i tried noepad,wordpad and office word but all 3 never shows them as they appears in the debugging window O_o
  • lonesome
    lonesome over 12 years
    you may laugh but i think from beginning you were right! when i send it to the debugging window it shows them in new line but in the file they are followed in same line! why is it like this? i tried noepad,wordpad and office word but all 3 never shows them as they appears in the debugging window O_o
  • lonesome
    lonesome over 12 years
    no, im using windows vista! ooh what you use to viweing them?
  • ewan.chalmers
    ewan.chalmers over 12 years
  • Olle Söderström
    Olle Söderström about 11 years
    If you're using windows it would be "\r\n" for a new line.
  • Huy Duong Tu
    Huy Duong Tu almost 11 years
    @lonesome: newline character in Linux is "\n". windows is "\r\n". if you try in windows. Please try "\r\n".getBytes()
  • yahya
    yahya over 8 years
    @OlleSöderström you should've write this solution as an answer! Thanks a lot.
  • Pang
    Pang about 7 years
    I don't see how this can be better than System.lineSeparator() or System.getProperty("line.separator").
  • OlgaMaciaszek
    OlgaMaciaszek almost 6 years
    You can also use System.lineSeparator() instead.