Writing a string array to file using Java - separate lines

前端 未结 5 640
不知归路
不知归路 2020-12-12 07:06

I\'m writing a program that writes sets of observations in the form of a String array (from User input) to file. I am able to write an observation to a .txt file and then ad

5条回答
  •  时光取名叫无心
    2020-12-12 07:32

    You need to push a line separator into the buffer.

    newLine();
    

    Here's the code

    for(int i = 0; i < observation.length; i++) {
      try (BufferedWriter bw 
            = new BufferedWriter(new FileWriter("birdobservations.txt", true))) {
        String s;
        s = observation[i];
        bw.write(s);
        bw.newLine();
        bw.flush();
      } catch(IOException ex) { 
        ex.printStackTrace();
      }
    } 
    

提交回复
热议问题