
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When to use the readAllBytes() method of InputStream in Java 9?
In this article, we will learn to use the readAllBytes() method of InputStream in Java 9. We will get to know the readAllBytes() method with its syntax, then we will learn about the InputStream Class with its methods, and lastly, we will learn the use of readAllBytes() with an example
What is the readAllBytes() Method?
In Java 9, the readAllBytes() method reads all bytes from an InputStream object at once and blocks until all remaining bytes have been read and the end of the stream is detected, or an exception is thrown. It returns a byte array containing the contents of the file.
The readAllBytes() method accepts only a single parameter as path, which is the path to the specified file. This method throws an IOException error if an I/O error occurs while reading from the stream.
Syntax
The following is the syntax for the readAllBytes() Method declaration:
public byte[] readAllBytes() (string path);
We can use the readAllBytes() method from the InputStream class to read all bytes into a byte array.
InputStream Class
The Java InputStream class is the superclass of all classes representing an input stream of bytes. Every subclass of InputStream always needs to introduce a method to return the next byte on request.
The following are some of the common methods InputStream class:
- mark(): This method marks the current position of the input stream.
- read(): This method reads the next byte of data from the Input Stream.
- reset(): This method repositions the input stream to the marked position.
Creating an InputStream constructor to fetch the file "demo.txt":
InputStream inputstream = new FileInputStream("c:\Temp\demo.txt");
When to Use the readAllBytes() Method?
The reallAllBytes() method can't automatically close the InputStream instance. When it reaches the end of a stream, the further invocations of this method can return an empty byte array. We can use this method for simple use cases where it is convenient to read all bytes into a byte array, and not intended for reading input streams with a large amount of data.
- When you need the entire content at once, e.g., parsing a file or processing an HTTP response.
- Use this method for streams that are opened, read once, and closed immediately.
- Can be used in Testing for reading small files, e.g., configuration files.
Note: Avoid using this method if the stream size is unknown or large because it loads everything into memory at once, which can cause an OutOfMemoryError.
Example to Read Content from a File Using readAllBytes() Method
Below is an example to read content from "Technology.txt" using the readAllBytes() method in Java:
import java.nio.*; import java.nio.file.*; import java.io.*; import java.util.stream.*; import java.nio.charset.StandardCharsets; public class ReadAllBytesMethodTest { public static void main(String args[]) { try(InputStream stream = Files.newInputStream(Paths.get("C://Temp//Technology.txt"))) { // Convert stream to string String contents = new String(stream.readAllBytes(), StandardCharsets.UTF_8); // To print the string content System.out.println(contents); } catch(IOException ioe) { ioe.printStackTrace(); } } }
Output
We have created a "Technology.txt" file in a "C:\Temp" folder with simple data: { "JAVA", "PYTHON", "JAVASCRIPT", "SELENIUM", "SCALA"}.
"JAVA", "PYTHON", "JAVASCRIPT", "SELENIUM", "SCALA"