New Line BufferedWriter Java

New Line BufferedWriter Java uses newLine() method to give new line in the destination file. Example given.

But before going into example and explanation, let us see what DataOutputStream will do with new line in the destination file. DataOutputStream does not have a builtin method. Infact, new line is OS dependent. \n works in System.out.print(). \n reads the new line character from OS and replaces \n. This we have seen with example in DataOutputStream New Line Java.

Now let us go for Example on New Line BufferedWriter Java
import java.io.*;
public class UsingNewLine
{
  public static void main(String args[]) throws IOException
  {
    BufferedWriter bw = new BufferedWriter(new FileWriter("def.txt"));

    bw.write("Good Morning");
    bw.write("Hyderbad");
    bw.newLine();
    bw.write("Good Morning");
    bw.newLine();
    bw.write("Hello World");
 
    bw.close();
  }
}

New Line BufferedWriter JavaOutput Screenshot as seen in def.txt

bw.write(“Good Morning”);
bw.write(“Hyderbad”);
bw.newLine();
bw.write(“Good Morning”);
bw.newLine();
bw.write(“Hello World”);

First two lines gets in a single line. And the last two in different lines due to addition of new line character with newLine() method.

Increase your Java knowledge with all View All for Java Differences on 80 Topics

Leave a Comment

Your email address will not be published.