Writing Data to File Using BufferedWriter and FileWriter

Start course
Overview
Difficulty
Beginner
Duration
1h 46m
Students
26
Description

In this course, we'll learn about the Java Input/Output subject.

Learning Objectives

  • API and Java I/O
  • OutputStream Class
  • InputStream Class
  • Reader Class
  • Writer Class
  • Serialization, Deserialization, and Non-Serializable objects

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi, there. In the previous video, we talked about the writer class. In this video, we'll do an example using the writer class. But before, let's look at the methods of this class. The basic method we use to write data using the writer class is the writer method. But there are different versions of this method. Now let's look at these in turn. The first type of the write method takes a parameter type of int. This method writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value. The 16 high-order bits are ignored. The second type of the write method takes a parameter type of string. This method writes a string. The third type of the write method takes a parameter type of char array. This method writes an array of characters. The fourth type of the write method takes three parameters.

The first parameter is the string. The second parameter is the offset from which to start writing characters. The third parameter is the number of characters to write. This method writes a portion of a string. The fifth type of the write method takes three parameters. The first parameter is the char array. The second parameter is the offset from which to start writing characters.

The third parameter is the number of characters to write. This method writes a portion of an array of characters. And the other method is the close method. This method closes the stream, flushing it first. Okay, now let's move on to the Eclipse and get some practice. First, I will create a new package and class. I right-click on the source folder in the Java I/O package and select the new class options. The package name can be writer example. And the class name can be writer example again.

And I will check the checkbox for the main method and click the 'Finish' button. Okay, first I will create a reference from the writer abstract class. Writer writer = new Filewriter. Since the writer class is an abstract class, we cannot use it directly. We use it subclasses. The file name we'll create can be writer.txt. Also, this class throws the file not found exception again. So I will use the throws keyword after the main method; throws IOException. Okay, now we'll create an object from the buffered writer class. Bufferedwriter bufferedwriter = new Bufferedwriter. And for the constructor parameter, I write the writer object. Okay, now I will get data from the user using the scanner class. Scanner scanner = new Scanner(System.in). First, I will show a message. System.out.printIn("Please enter a text:"). Now I will get this data and assign it to a string. String data = scanner.nextline(). Now we can write this data to the file using the buffered writer object. bufferedwriter.write(data). And let's show a message again. System.out.printIn("Data is written"). Lastly, I will close the stream. scanner.close(). bufferedwriter.close(). writer.close(). Okay, now we can run the app. I'll enter a text. I write, "Hi, Java Developers".

After pressing the 'Enter' key, the data is written to the file. Let's check it. I right-click on the project folder and select the 'Show In' system explorer option, and I open the project folder. And here is the writer.txt file. I'll open it. And the text is here. Okay, now let's write only the Java word. For this, I will add two more parameters to the write method. The second parameter will be offset. So the first letter of the Java word is J, and the index number of the J is three. So the offset will be three. And I want to write the Java word. So, the java word is four characters. So the third parameter will be four. Okay, let's run and test it again. I write the "Hi Java Developers" text again. After pressing the 'Enter' key, the data is written to the file. Let's check it. I'll open the writer.txt file. And notice only the java ward is written this time. Okay, so the usage of the writer class is like that. Let's take a short break here. See you in the next video.

 

About the Author
Students
512
Courses
47
Learning Paths
3

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics