It is necessary to write the lines from the array into a text file, each with a new line. I have already added line breaks everywhere \ n - but for some reason everything is written in one line.

String[] list = { "Строка 1 \n", "Строка 2\n", "Строка 3\n"}; BufferedWriter writer = new BufferedWriter(new PrintWriter("C:\\javaproject\\someFIle.txt")); for (String s : list) { writer.write(s + "\n"); } writer.close(); 

    2 answers 2

    Notepad to transfer the line is used \r\n

      I will add that if you want to put the transfer of lines regardless of the platform, you can use several ways

      1. Use formatting and one of its code characters. In particular, %n , for example

         Calendar c = ...; String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY%n", c); ^--- 

        n - line separator, which "draws" it in different ways, depending on the platform on which the script is running

        For more details see documentation.

      2. Use System.getProperty("line.separator") ( docks ). Which also places a separator depending on the platform.

        In order not to write such a giant structure every time, you can take it somewhere

         final String sp = System.getProperty("line.separator"); 

        and then use

         writer.write(text + sp); 
      3. Although in Java7 you can write this way:

         System.lineSeparator(); 

        docks


      Another variant of the BufferedWriter itself, use newLine();

       writer.write(s); writer.newLine();