Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,167 questions

40,724 answers

573 users

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 23 views
1 answer 32 views
32 views asked Aug 11, 2023 by avibootz
2 answers 118 views
118 views asked Jun 22, 2020 by avibootz
2 answers 124 views
...