Reading Data Using the Reader Class
Start course
1h 46m

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


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

Hi there. In the previous video, we talked about the Reader class. In this video, we'll do some practice with the Reader class. If you're ready, let's start. First, I'll create a new class. I right click on the source folder and select the New Class options. The package name can be readerrxample. The class name can be ReadDataUsingReader. And I'll check the checkbox for the main method and click the 'Finish' button. Okay.The

exercise we do will be as follows. First, we'll write data to a file with FileOutputStream and then, we'll retrieve this data back into the application using the Reader class. If you're ready, let's start. First, I'll use the Scanner class to get the data. Scanner inputScanner = new Scanner( And I'll show a message to the user. sout, double quotes "Please enter a message:" Now, let's get this message and assign it to a String container. String message = inputScanner.nextLine( ). Now, let's create an object from the FileOutputStream class. FileOutputStream fos = new FileOutputStream. The file name can be message.txt. Also, we need to create a try-catch block here.

I select the surround with try-catch option from the quick fix dialog. Okay. Let's continue. Now, I'll create a byte array and assign the message to this array. If you remember, the FileOutputStream class was transferring data on a byte basis. For this, we assign the message as byte to the byte array. byte[ ] messageArray = message.getBytes( ). And lastly, to write the message into the file, I will use the write method of the FileOutputStream class. fos.write(messageArray). Also, since the write method throws the IOException, we need to add a catch block. So, I select the add catch clauses option from the quick fix dialog. Okay. At this stage, the message has been printed to the message.txt file. I'll also print a message to the console screen that the write is complete. sout, double quotes, "Data is written to the file." Let's run the code at this stage. I'll enter a message like that. This is the usage of the Reader class. And I'll press the 'Enter' key. Okay. So now, the process is complete. Now, let's look at the file. I right click on the 'Java Input Output' package and select the show in system file options. I open this project and as you can see, there's a file named message with the extension txt.

I open this file. And as you can see, the message is here. Yes, now it's time to read this data and write it on the console screen using the Reader class. First of all, I want to put some waiting time after the data is written so that we can observe the transition phases more easily. Therefore, right after the message here, I'll call the Thread.sleep method. Waiting here for half a second will suffice, that's why I'm typing 500 here. You can also use the L suffix here because the parameter is actually of type long. When you don't use it, it will be converted to long automatically, but it's more convenient to use. Also, we need to add another catch clause. I'll use the quick fix dialog for this. Now, let's show a message that the data will be read. sout, double quotes, "Data will read soon, please wait..." And now, I'm going to put a two-second sleep again. So, I'm going to copy the sleep method here and paste it here. The duration of the sleep will be two seconds, i.e., 2,000 milliseconds. These are not mandatory but will allow us to better observe the processes on the console screen. Now, let's show the message data is reading. sout, double quotes, "Data is reading... " Also, this will be the print method not the println.

Here too, we'd better wait for half a second. I will copy and paste the sleep method here. Yes, after these operations, we can now use the Reader class. First, let's create a reference from the Reader class. Reader myReader, of course, since the Reader class is an abstract class, we'll use the subclasses of this class. Therefore, after the equal sign, I'm writing the new FileReader. We should write the name of the file to be read in parentheses with its extension. We'll read the message.txt file. Therefore, I'm copying the message.txt and I'm pasting it here. Also, I'd like to point out that when creating a new object using the Reader class, we get similar errors like FileNotFoundException and IOException as above. But now that we have created the try-catch block for these errors and now, we are in the try block, we do not get any errors. But keep in mind that if you build it outside of the try-catch block, you will get similar compilation errors. Let's continue now. Yes, now let's try to read the data in order by using the do-while loop again. But first, let's find out if the file is ready for reading.

It's possible to find out using the ready method of the Reader class. Returns true if the file is ready to read, false if otherwise. So, let's create an if structure here. if(myReader.ready( ) ). Yes, we can do the necessary actions here. I will create the do block. I call the read method of the Reader class inside the do block, ). Of course, if you remember, the read method will read the data character by character but will return the int value corresponding to the character it read back. For example, 65 for the capital letter A. It will also return -1 after all the characters have been read, that is, when there is no more data to read. Therefore, I'll pass this value to an int variable again and create an if statement here. Let's create an int variable named dataRead just outside the do-while loop. Now, let's check if it is -1 with the if block. if(dataRead != -1). In this case, it means that the character has been read and the integer value of the related character is transferred to the dataRead variable. So now, we can print this character to the console. sout(dataRead). Also, this will be again the print method not println.

Of course, if you leave it like this, you will see the integer value corresponding to that character instead of the character on the console screen. Therefore, we'll perform a cast here and print it to the console as a character. Yes, after this step, the data will be written to the console. If you want, let's put some sleep time after printing each character to the console. Thread.sleep(200L). And lastly, let's close the OutputStream, Reader stream and the Scanner. fos.close( ), myReader.close( ), inputScanner.close( ). Now, let's create the while condition. This loop should continue until all characters in the file have been read. We can understand from the -1 value that returns when all the characters are finished. Therefore, I'll write the statement in the if condition here as well. So, if there is data to read, the loop will continue. If the data to read runs out, then this condition will be false and the loop will terminate. Also, you can create the else block and show an error message to the user. Okay, so the loop is ready. But before running the app, I'll print a message to indicate that the process of reading the data is finished. And after the outer if block, first I'll create an empty println method to move the cursor to the next line, and now I'll create the other message.

sout, double quotes, "The reading process is complete." Yes, the app is ready but when we run it this way, we'll get an error because we closed the streams inside the do block. So, after the first loop, the stream will be closed and in the second loop, we will get an error because there is no stream to be used anymore. If you want, let's run the application like this and see the error. Let's run and test it. As the data is to be written to the file, I can use this message. This is the usage of the Reader class. I press 'Enter' and the write operation is complete and the IOException has occurred. As we mentioned before, if you notice, we closed all the streams in the do block with the close method. So, in the second loop, we got an error because there is no stream to be used, that is, to read. Let's fix this now. It's sufficient to exclude the close methods here from the loop. Yes, now let's run it one more time. As the data is to be written to the file, I'll use the same message. This is the usage of the Reader class. I press 'Enter' and the write option is complete. After waiting for a while, the process of reading the data will start. And as you can see, the data is currently being read character by character and printed to the console. And the reading process is complete. Yes, this is how the Reader class is used. It's actually very similar to the InputStream class in terms of usage. The only difference is that while this class reads character-based data, the InputStream class reads byte-based data. Yes, let's take a short break here. See you in the next lesson.


About the Author
Learning Paths

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