Java Input Output Operations
In this course, we'll learn about the Java Input/Output subject.
- API and Java I/O
- OutputStream Class
- InputStream Class
- Reader Class
- Writer Class
- Serialization, Deserialization, and Non-Serializable objects
- 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 InputStream class. In this video, we'll do some practice with the InputStream 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 inputstreamexample, the class name can be ReadData, and I'll check the checkbox for the main method and click the 'Finish' button. Okay, in the previous video, we created a file named test.txt and we wrote a text in this file. In this video, let's try to read this file and show the text on the console. First, I'll use the read method which is parameterless. If you remember, if there is no data to read, the read method returns -1. We read the data using this information in the do-while loop. First, I'll create an object, InputStream myInputStream = new FileInputStream( ).
For the constructor parameter, we have to write the name of the file with its extension, so here I write "test.txt" in double quotes. If you want, you can create a constant again and call that constant here, and the FileInputStream throws a FileNotFoundException. For this, we need to create a try-catch block. I select the surround with try-catch option from the quick fix dialog. Now, I'll use the available method to estimate how many bytes are in the file. int length = myInputStream.available(). Also, the available method throws the IOException. So, I'll add another catch block using the quick fix dialog. Okay, I will print this length variable on the console. S.out.println( "Estimated bytes in the file: " + length). Okay, now I will create a do-while loop.
In the do block first, I'll check if the file has data to read. Remember if there was no data to read, the read method returned -1. Therefore, I will first perform this check using the if block but first, I'll create an int variable above, int dataRead. Now, I will assign the myInputStream.read to this variable in the do block. dataRead = myInputStream.read(). Now we'll create the if statement, if(dataRead != -1). If the file has data to read, I'll assign this to a char variable in the if block. char letterRead = dataRead. Of course, this method returns the integer value of the letter it reads. For example, if it reads the capital letter A, it will return 65. If it reads the lowercase letter a, it will return 97 because the characters in the char data type have an integer equivalent. As a result, we have to convert this integer value into character data type. Therefore, I'll apply cast operation here. Yes, we now have one character. Now we can print this directly to the console or we can convert the character to a string and print it to the console as a string. It doesn't matter. Let's print it as a string if you want. Let's create a string for this. String data = String.valueOf(letterRead). Now we can write the data on the console. S.out.print(data). Also, this will be the print method, not println.
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 the condition will be false and the loop will terminate. Okay, so the loop is ready, but before running the app, I'll print two messages on the console. The first is to show the user that data is being read. The other is to indicate that the process of reading the data is finished. Just above the loop, I write S.out.print( "The data is reading..."). Also, this will be the print method, not println.
And after the loop, first I will create an empty println method to move the cursor to the next line, and now I'll create the other message. S.out.println("The reading process is complete.") andI'll close the InputStream. Okay, now we can run the code and test it. As you can see, the Hi Java Developers is printed on the console. Of course, this process happens so fast that we can only see the result. If you want, let's wait a little bit after each character is printed on the console. Thus, you can more easily see that the reading process is done byte-by-byte. Right after the print method in the if block, I'll call the Thread.sleep method. I also write 300 as the parameter. That's 300 milliseconds. So, it will wait for about one third of a second.
Of course, we need to put this in the try-catch block. Therefore, I choose the add catch clause to surrounding try option from the quick fix dialog. Yes, now let's run the application once again. As you can see, it now waits a little bit after each character and then writes the other character to the console, and after all the data has been read, the statement showing that the reading process is finished is printed on the console. Also pay attention, we have also printed the number 18, that is, the total number of bytes, that is, the total number of characters in the file to the console with the available method. Yes, the read method is like this. I think you understood that the read method reads byte-by-byte.
Now let's look at the read method which is with the parameter. For this, it'll be enough if we turn the do-while loop into a comment line. Other codes can remain the same. Also, I'll convert the catch block here to the comment line. Okay, let's continue. Now, I'll call the read method of the InputStream class, myInputStream.read, and this time I'll give an array type of byte. So, let's create this array, byte dataList = new byte. Also, we need to specify the length of this array. So, you can type here an int value, for example, 100 or you can type the estimated number of bytes. So, we assigned it to the length variable. So, I write the length here. Okay, now I pass this array to the read method.
Now, I'll create a new string and convert this byte array to the string to print on the screen, String dataString = new String( ), and inside the parentheses, I write the name of the byte array i.e., dataList. Okay, now I will print the string on the console. S.out.print(dataString). Also, this will be the print method again to write the characters side-by-side, and here, I'll show the user a message that data is being read. S.out.print( "The data is reading..."). Also, I'll use the print method instead of the println again. Okay, let's run and test it. As you can see, the length of the available byte is printed on the console. Then, the data is reading message, then the text in the file, and lastly, the completed message is printed. But I made a typo here, I'll fix it right away and get it working again. Okay, now let's only print the Java word to the console. For this, in the constructor parameter of the String class, I put two more parameters. The second parameter will be the offset. So, the index number of the J letter is 3, so I write 3 as the second parameter. The third parameter will be the length of the data that will be read. So, the length of the Java word is four. So, I write 4 here.
Okay, let's run it again. As you can see, this time only the Java word is printed on the console. Okay, lastly, I want to show you how you can read the data byte-by-byte with this read method. To do this, I will use the for loop this time, for(int offset = 0; offset < length, i.e., number of the byte in the file; and offset++), and I'll move these two lines in the for loop. This time, the second parameter will be offset and the third parameter will be 1 because in each iteration, the offset will increase but we will only read one byte. For example, in the first iteration, the offset is 0 and the third parameter is 1. So, the H letter will be printed on the console. In the second iteration, the new value of the offset will be 1 and the third parameter is again 1. So, this time the letter in the first index will be read, i.e., the E letter is printed on the console.
In the third iteration, the new value of the offset will be 2 and the third parameter is again 1. So, this time, the letter in the second index will be read, i.e., L letter is printed on the console, and go on like that. When the value of the offset will be 18, the condition will be false, and the loop will terminate. Also, I'll add the Thread.sleep method here to observe the read process. The parameter can be 300 milliseconds again. Of course, we need to put this in the try-catch block. Therefore, I choose the add catch clause to surrounding try option from the quick fix dialog. Okay, let's run and test it. As you can see, reading is done byte-by-byte and the reading process is complete. Yes, you can read data this way using the InputStream class. I hope it's been a useful lesson for you. Let's take a short break here, see you in the next lesson.
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.