Java

Java BufferedWriter

The topic under discussion in this article is the BufferedWriter class which is used to write the data in different formats of streams in files. The BufferedWriter class is dependent on the File writer class because the object of the Buffered writer has the object of the FileWriter class as the parameter value. There are several functions in the BufferedWriter class that allow us to write and edit the file to our liking. We will implement several examples where we will use the BufferedWriter class function.

Example 1: Inserting a String Variable Into a Text File Using the Write() Function

In this example, we will insert a string variable into a text file using the write() function of the BufferedWriter class and the object of the FileWriter class.

In the given code, we can see that the packages of both classes are imported which we will use in our program. We create a class for our program and in the main function. We start by initializing a string variable. Then, in the try and catch parentheses, we create the objects for both the FileWriter and BufferedWriter class. We pass the name of the file as the parameter in the FileWriter class’s object.  We pass the object of the FileWriter class in the BufferedWriter class’s object. After this, we call the write() function with the BufferedWriter class’s object and pass the string variable as the parameter. Then, we terminate the BufferedWriter using the close function.

Now, we open our terminal and compile our Java program. Then, we execute it. The success message is shown on the terminal once the code is executed. Now, we open our text file to confirm that the string variable is inserted correctly in our text file.

As we can see in the previous snippet, the string variable is inserted in our text file.

Example 2: Inserting a Character Array Into a Text File Using the Write() Function

We will insert a character array into a text file using the BufferedWriter class’s write() function and the FileWriter class’s object in this example.

In this program, we import the IO packages of the FileWriter and BufferedWriter in Java. Then, we create a class in which the function is scripted. In the main function, we first initialize a char array variable named “a” and assign it a value as well. Then, we write a try and catch statement in which all the functional code are scripted. We start with the creation of the object of the FileWriter class where the text file is passed as a parameter. Then, we create another object of the BufferedWriter class where we pass the previous object of the FileWriter class that we created earlier. After this, we use the BufferedWriter’s class object to call the write() function. The success message is defined after the function using the println function. The try statement ends with the close() function of the BufferedWriter class. In the end, the catch statement handles the  exceptions. After saving the program, we can execute the program in our Ubuntu terminal using the following commands:

When the program is executed successfully, the success message appears on the terminal. For further verification, we also look into our text file.

As we can see, the char variable was successfully passed in the text file as one line.

Example 3: Inserting Multiple Lines of Text Into a Text File Using the New Line() Function

We will insert multiple lines of text into a text file using the BufferedWriter class’s new line() function and the FileWriter class’s object in this example.

In this program, we import all the IO packages by writing the “*” character. Then, we create a new class in which the try and catch statement are written inside the main function. In the try statement parenthesis, the core functionality of the program is written. In the try statement, we start by creating the objects of the FileWriter class and the BufferedWriter class of Java.

The text file name is passed in the FileWriter’s class object. This object is then be passed in the initialization statement of the BufferedWriter class. Then, we use the “bw” object to call the write function to pass a string into the text file. The string is directly written using the inverted commas. After this, we call the new line() function to create a line break in the text file.

Then, we add two more lines using the write() and new-line() functions simultaneously. The close() function stops the BufferedWriter object. The success message to display on the terminal is also be added. At the end of the program, we write the catch statement to handle any exceptions.

Now, we save the program and open our terminal. Then, we compile and execute this program to add these multiple lines to our text file. As the program is executed, the lines are added to the text file and the success message is visible on our terminal. We can also verify the addition of these lines by opening our text file.

As indicated in the previous figure, multiple lines were added to our text file.

Example 4: Inserting a String Variable Into a Text File and Flushing the Writer Using the Flush() Function

We will insert a string into a text file and flush the writer using the BufferedWriter class’s flush() function and the FileWriter class’s object in this example.

We start by building a class and a try/catch block for our script. Then, within the try/catch block, we start by creating objects for the FileWriter and BufferedWriter classes and initializing a string variable. The file name is sent as a parameter to the FileWriter class’s object. The FileWriter class’s object is passed to the BufferedWriter class’s object. After that, we use the object of the BufferedWriter class to perform the write() method before executing the flush() function with the same object. The BufferedWriter is then terminated with the close function.

Then, we launch the terminal, build and run our Java application. After the program is completed, a message is presented on the console indicating that the writer has been flushed. We now open our text file and check to see if the string variable was properly entered as shown in the following:

Conclusion

We discussed about the BufferedWriter class of Java in this article. This class is normally used with the FileWriter class to insert the data into files in the output streams. There are multiple functions in this class that were discussed in this article. We also implemented several examples of the write(), new line(), close() ,and flush() methods of the BufferedWriter class.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.