This video tutorial explains the code and shows you to write to a test file in Java. Program uses the BufferedWriter class to write to text file efficiently.
This video tutorial first creates an Eclipse project and then explains how to write the source code to write to a text file. First it explains the write one line to a text file then it teaches you to modify the same code to write 1000 lines of text data to the same file.
Program uses the FileWriter class along with the BufferedWriter class for efficient writing to text file.
Following is the code for creating the object of FileWriter class:
FileWriter fstream = new FileWriter("D:\\roseindia\\roseindiatutorials-videos\\out.txt");
Following code is used to create the object of the BufferedWriter class:
BufferedWriter out = new BufferedWriter(fstream);
Finally following code is used to write a line to text file:
out.write("Hello From Java Program");
In last ouput stream is closed to close the text file.
Here is the video instruction of the same:
Here is the video tutorial of "How to write to a file in Java?":
Download the source code from the website http://www.roseindia.net/java/beginners/java-write-to-file.shtml
About the Java.io.BufferedWriter class
This class is used to write the text to a text stream by buffering the characters to provide the efficiency in the writing process. This class is useful when you have to write large number of text data to a text file. Class gives maximum performance as it is using the buffering technology while writing.
While using the BufferedWriter class you can also provide the buffer size, but the default buffer size is sufficient for almost all the programming work.
This class is using buffer and sends the output data immediately to the character or byte steam, which is being used in the process.
Constructors of the BufferedWriter class
This constructor takes the Writer as parameter and constructs the buffered character-output stream.
BufferedWriter(Writer out, int sz)
This constructor takes the Writer as parameter along with the buffer size and constructs the buffered character-output stream.