How to write new line (line separator) to file in Java

2 Answers

0 votes
package javaapplication1;
 
import java.io.*;
 
public class Example {
     
    public static void main(String[] args) throws IOException {
                
        try (Writer writer = new BufferedWriter(new OutputStreamWriter(
              new FileOutputStream("d:\\data.txt"), "utf-8"))) 
        {

            writer.write("line 1" + System.lineSeparator() + "line 2");
        }
        catch(IOException ex)
        {
            System.out.println(ex.toString());
            System.exit(1);
        }
    }
}
  
/*
run:
   

   
*/

 



answered May 13, 2016 by avibootz
0 votes
package javaapplication1;
 
import java.io.*;
 
public class Example {
     
    public static void main(String[] args) throws IOException {
                
        try (Writer writer = new BufferedWriter(new OutputStreamWriter(
              new FileOutputStream("d:\\data.txt"), "utf-8"))) 
        {
            writer.write("line 1");   
            writer.write(System.lineSeparator());
            writer.write("line 2");
        }
        catch(IOException ex)
        {
            System.out.println(ex.toString());
            System.exit(1);
        }
    }
}
  
/*
run:
   

   
*/

 



answered May 13, 2016 by avibootz

Related questions

1 answer 171 views
1 answer 229 views
229 views asked Aug 11, 2023 by avibootz
2 answers 392 views
392 views asked Jun 22, 2020 by avibootz
2 answers 360 views
1 answer 247 views
247 views asked Nov 15, 2016 by avibootz
1 answer 251 views
...