Java BufferedWriter newLine() Method
Example
Write two lines to a file, and read them back:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("filename.txt"));
bw.write("First line");
bw.newLine();
bw.write("Second line");
bw.close();
BufferedReader br = new BufferedReader(new FileReader("filename.txt"));
System.out.println(br.readLine());
System.out.println(br.readLine());
br.close();
}
}
Definition and Usage
The newLine() method writes a line separator (the characters that end a line of text), so the next text starts on a new line.
The line separator depends on the operating system: \r\n on Windows, and \n on Linux and macOS. newLine() always writes the right one for the computer that runs the program.
Syntax
public void newLine()
Parameter Values
None.
Technical Details
| Throws: | IOException - if the writer is closed, or if another I/O error occurs. |
|---|
Related Pages
❮ BufferedWriter Methods