Friday, January 16, 2026
HomeLanguagesJavaBufferedWriter newLine() method in Java with Examples

BufferedWriter newLine() method in Java with Examples

The newLine() method of BufferedWriter class in Java is used to separate the next line as a new line. It is used as a write separator in buffered writer stream.

Syntax:

public void newLine()
            throws IOException

Parameters: This method does not accept any parameter.

Return value: This method does not return any value.

Exceptions: This method throws IOException if an I/O error occurs.

Below programs illustrate newLine() method in BufferedWriter class in IO package:

Program 1:




// Java program to illustrate
// BufferedWriter newLine() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create the string Writer
        StringWriter stringWriter
            = new StringWriter();
  
        // Convert stringWriter to
        // bufferedWriter
        BufferedWriter buffWriter
            = new BufferedWriter(
                stringWriter);
  
        // Write "A" to buffer writer
        buffWriter.write(65);
  
        // Revoke newLine() method
        buffWriter.newLine();
  
        // Write "B" to buffer writer
        buffWriter.write(66);
  
        buffWriter.flush();
  
        System.out.println(
            stringWriter.getBuffer());
    }
}


Output:

A
B

Program 2:




// Java program to illustrate
// BufferedWriter newLine() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
        // Create the string Writer
        StringWriter stringWriter
            = new StringWriter();
  
        // Convert stringWriter to
        // bufferedWriter
        BufferedWriter buffWriter
            = new BufferedWriter(
                stringWriter);
  
        // Write "GEEKS" to buffered writer
        buffWriter.write(
            "GEEKSFORGEEKS", 0, 5);
  
        // Revoke newLine() method
        buffWriter.newLine();
  
        // Write "GEEKSFORGEEKS"
        // to buffered writer
        buffWriter.write(
            "GEEKSFORGEEKS", 0, 13);
  
        buffWriter.flush();
  
        System.out.println(
            stringWriter.getBuffer());
    }
}


Output:

GEEKS
GEEKSFORGEEKS

References:
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedWriter.html#newLine()

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32470 POSTS0 COMMENTS
Milvus
117 POSTS0 COMMENTS
Nango Kala
6838 POSTS0 COMMENTS
Nicole Veronica
11972 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12055 POSTS0 COMMENTS
Shaida Kate Naidoo
6975 POSTS0 COMMENTS
Ted Musemwa
7214 POSTS0 COMMENTS
Thapelo Manthata
6928 POSTS0 COMMENTS
Umr Jansen
6906 POSTS0 COMMENTS