In the section of Java Tutorial you will learn how to write java program to write to a file.In the section of Java Tutorial you will learn how to write java program to write to a file.
Learn how to write to a file from Java program. This tutorial teaches you how you can use the OutputStreamWriter on a FileOutputStream classes to write data to a file from Java program.
In the section of Java Tutorial you will learn how to write java program to write to a file. We will use the class FileWriter and BufferedWriter to write to a file.
The FileWriter is a class used for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.
The BufferWriter class is used to write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
Here is the video tutorial of "How to write to a file in Java?":
Here is the code of java program to write text to a file:
If you execute the above code program will write "Hello Java" into out.txt file. While creating the object of FileWriter class we have passed the name of the file e.g. "out.txt" as constructor argument.
The BufferedWriter class takes FileWriter object as parameter. The write() method of the BufferedWriter class actually writes the data into the text file.