In this section, you will learn how to write the text in new line of text file.
In this section, you will learn how to write the text in new line of text file.In this section, you will learn how to write the text in new line of text file.
Description of code:
The package java.io.* has provide many input and output streams for accessing the text files. In order to write a text in the file, there are different output streams. Among them, BufferedWriter has provide a method newLine() which writes the text into another line.
You can see in the given example, we have created an object of BufferedWriter class that uses the FileWriter class to parse the file and create it. The method write() adds the simple text to the file. Then the nextLine() writes a line separator and allow to write next text to another line in the file.
Here is the code:
import java.io.*; public class FileNewLine { public static void main(String[] args) throws Exception { File f = new File("C:/file.txt"); BufferedWriter bw = new BufferedWriter(new FileWriter(f, true)); bw.write("Hello"); bw.newLine(); bw.write("How are you?"); bw.close(); } }
Through the method newLine(), you can write the text to another line.
Generated File data:
Hello How are you? |