Home Tutorial Java Core Files Java file new line

 
 

Share on Google+Share on Google+
Java file new line
Posted on: May 2, 2006 at 12:00 AM
Advertisement
In this section, you will learn how to write the text in new line of text file.

Java file new line

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?
Advertisement

Related Tags for Java file new line:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 2, 2006

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com