FileReader and LineNumberReader 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 our previous lesson, we talked about the Reader class and did a little exercise using this class. If you remember, the Reader class was an abstract class, and we were using subclasses to use this class. We've already done the necessary reading operations by using the FileReader subclass in our previous lesson. In this lesson, we'll talk about another subclass LineNumberReader. This class is also a subclass of the Reader class. It doesn't actually inherit directly from the Reader class. It inherits from the BufferedReader class, which is a subclass of the Reader class. The purpose of this class is to read line by line and return the number of the line it reads. So, thanks to this class, we can read a file line by line.

Now let's look at the methods used in this class. In fact, since this class is a subclass of the Reader class, it can already use the methods we learned in the previous lesson, such as read(), skip(), and close(). The three methods in the table are the methods belonging to this class. Let's examine them now. The first method is the readLine() method. This method reads a line of text. When a line terminator is read, the current line number is incremented. A line terminator does not mean a dot, but a new line. For example, when you enter a line in text with the Enter key, it is now perceived as a new line. Or, when you use \n or \r, which is used to switch to a subline in a textual expression using coding, it is also perceived as a new line. Pay attention to this. If it reaches the end of the line that does not contain any line terminator, it returns null. It also returns IOException as an error.

The other method is the getLineNumber() method. This method returns the line number. By default, line numbering starts with zero. When the line reaches the terminator, this value increases by one. So, it actually returns one for the first line. The last method is the setLineNumber() method. This method changes the current line number with the value int given as the parameter to it. Note, however, that the setLineNumber() method does not actually change the current position in the stream. It only changes the value that will be returned by the getLineNumber() method. So, let's move on to the Eclipse and get some practice. First, I will create a new class. So, I right click on the 'readerexample' package and select the 'New', 'Class' options. So, the class name can be LineNumberReaderExample. And I'll check the checkbox for the main method and click the 'Finish' button.

Okay. In this example, we read the data in the message.txt file which we created in the previous video. First, I will change the data in this file. So, I'll open the message.txt file. I want to paste a draft text here. You can find draft documents on the Internet. For example, if you type lorem ipsum in your browser, you can access readymade texts that you can test. I click on this site. And I'll copy the first paragraph here and paste it into the message.txt file. Yes, if we leave it that way, it will assume there are only two lines. Because if you pay attention, although there is a dot at the end of the sentences, the bottom line is not passed. Therefore, I'll move each sentence to a lower line with the Enter key. Yes. Now, let's save our file. Now we can continue through Eclipse. First, I'll create an object from the FileReader class. FileReader fileReader=new FileReader. The name of the file will be message.txt. Also, I'll add the try-catch block.

If you want, instead of the try-catch, you can use the throws keyword just after the method name, i.e., main method. This is also valid. Okay. Now we'll create an object from the LineNumberReader class. LineNumberReader lineNumberReader=new LineNumberReader. And for the constructor parameter takes a Reader object. So, I will write the fileReader object. Okay. Now we'll create two variables. One will be of type string and we will use it to store the data in each row read. The other will be to store the lineNumber of the lines to read. So, I write String data, int lineNumber. Now I'll create a do-while loop to read the data in the file. In the do block, first I will assign the readLine() method of the LineNumberReader class to the data variable. data=lineNumberReader.readLine(). If the data to be read ends and no line terminator is encountered, the readLine() method returns the null value.

Therefore, we can perform a check with the if statement here. If the data is not equal to null, in this case, both the read data will be transferred to the data string, and now we can get a lineNumber back. Now let's pass the line number to the lineNumber variable. lineNumber=lineNumberReader.getLineNumber(). If you remember, the getLineNumber() method was returning the line number of the line read. Now, let's print the lineNumber and the data on that line to the console. sout("Line" +lineNumber+ ":" +data). 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 null value that returns when all the lines 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, the readLine() method may throw the IOException. For this, we need to add a try-catch block, or we can use the throws declaration again. So, this time, I select the 'Add throws declaration' option. And it added the throws declaration to the main method. But if you notice, only the IOException is added here because the IOException class is already a super class of the FileNotFoundException class. Therefore, it will be sufficient to add the IOException here. And lastly, I'll close the strings outside the loop. fileReader.close() lineNumberReader.close(). Everything is ready. Let's run and see the output. As you can see, the number of each line is printed at the beginning of the corresponding line. The number of the first line is 1 and the number of the last line is 5. This is how the LineNumberReader class is used. 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