Java

Java Input Stream ReadAllBytes

In this article, we will discuss the ReadAllByte’s function present in the Java input stream. The ReadAllBytes function is used to read all the content of a file and can be used with bin and txt extension files. In this article, we will discuss and implement the different uses of the ReadAllBytes function in the Ubuntu 20.04 environment.

Example 1: Using ReadAllBytes Function to Read a Text File

In this example, we will read all the content of a text file using the ReadAllBytes function. The content of the file is transferred to the byte array variable which is then printed to display the content of the file in this example.

In the given code, we start by creating a class in which the main function is created. In the main function, we adopt the try and catch approach to achieve our objective. In the “try” parentheses, we initialize a byte[] variable named “content” which calls the file class and uses its ReadAllBytes function. We use the paths.get() method as a parameter in this function. We enter the name of the file that we wish to access within this function, which in our instance is “file.txt”. After this, we pass the byte[ ] variable into the “System.out.println” function, which displays the contents of the text file that we specified earlier.

Now, we move on to the terminal where we compile and execute the previous code to display all the bytes of our text file. As we can see in the following output, after executing the code, we get an output in which the contents of the text file are written.

We can also verify that the content is the same by opening the text file and comparing it as we see in the snippet. The output and the content are also the same.

Example 2: Writing a Text File and Using the ReadAllBytes Function to Read the Text

We will write a text into a text document and use the read all bytes function to read the whole contents of that document in this example. The document’s content is moved to the byte array variable which is subsequently printed to display the document content. The write function is used to add a text to the document in this example.

In the following code, we develop a class in which the main function is generated. We use the try and catch strategy in the main function to achieve our goal. The write() method of the files class is referenced in the first try and catch parentheses. The text is inserted into the file using the files class function. We establish a byte[] variable named “content” in the second try and catch parentheses which calls the file class and uses its ReadAllBytes function. The paths.get() method is used as a parameter in this function. We enter the name of the file that we wish to access within this function, which in our instance is the “file.txt.” The byte[] variable is then sent to the “System.out.println” method which shows the contents of the text file that we previously requested.

Now, we open the terminal, build and execute the preceding code to insert the content into a document and show all the bytes in that document. As seen in the previous snippet, after running the code, we obtain an output that contains the contents of the text file.

We can also confirm that the content is the same by opening the text file and comparing it to the output which shows that the output and content are identical.

Example 3: Using ReadAllBytes Function to Read a Binary File

In this example, we will read a binary file using the ReadAllBytes method. The binary file’s data is copied to the byte array variable, which is then printed to display the document’s content.

In the following code, we create a class that produces the main function. And we utilize the try and catch technique in the main method to achieve our purpose. In the try and catch parentheses, we create a byte[] variable called “content” which calls the file class and utilize its ReadAllBytes function. This function takes the paths.get() method as an argument. Within this function, we input the name of the file that we want to access, which in our case is “file.bin”. The byte[] variable is then sent to the “System.out.println” function which displays the contents of the binary file that we previously called.

Now, we open the terminal and run the previous code to display all of the bytes in the binary file. After running the code, we get an output that contains the contents of that file, as seen in the following sample:

We can also validate the content by opening the text file and comparing it to the result, which demonstrates that the output and content are the same. 

Example 4: Using ReadAllBytes Function in the Return Statement to Read a String

In this example, we will use the ReadAllBytes method to extract the content from a string variable.

In the following code, we construct a class with the main function and use the try and catch approach in the main method to accomplish our goal. We first define a string variable and build an object of the input stream class that converts the string to a stream by supplying it as a parameter. We write the return statement in the try parentheses in which the object of the input stream class executes the ReadAllBytes method to show the value of a string variable.

Then, we open the terminal and execute the preceding code to show all the bytes in a string variable:

Conclusion                              

We discussed the ReadAllBytes function of the files class in Java in this article. We implemented this function to get the contents of a text file, binary file, and even a string variable. The examples were implemented in the Ubuntu 20.04 environment where we used this function while declaring the variables and even called the function in the return statement as well.

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.