
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
When to use the readAllBytes() method of InputStream in Java 9?
Since Java 9, we can use the readAllBytes() method from InputStream class to read all bytes into a byte array. This method reads all bytes from an InputStream object at once and blocks until all remaining bytes have read and end of a stream is detected, or an exception is thrown.
The reallAllBytes() method can't automatically close the InputStream instance. When it can reach 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.
Syntax
public byte[] readAllBytes() throws IOException
In the below example, we have created a "Technology.txt" file in a "C:\Temp" folder with simple data: { "JAVA", "PYTHON", "JAVASCRIPT", "SELENIUM", "SCALA"}.
Example
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
"JAVA", "PYTHON", "JAVASCRIPT", "SELENIUM", "SCALA"
- Related Articles
- When to use the readNBytes() method of InputStream in Java 9?
- Importance of transferTo() method of InputStream in Java 9?
- When to use the delayedExecutor() method of CompletableFuture in Java 9?
- When to use the ofNullable() method of Stream in Java 9?\n
- When can we use StackWalker.getCallerClass() method in Java 9?
- When to use fillInStackTrace() method in Java?
- What is the use of the toEpochSecond() method in Java 9?
- When to use the ServiceLoader class in a module in Java 9?
- What is the use of the Optional.stream() method in Java 9?\n
- How to use the collect() method in Stream API in Java 9?
- When can we use the pack() method in Java?
- When can we use the getClass() method in Java?
- convert InputStream to ByteArray in java
- When can we use intern() method of String class in Java?
- Java Program to Convert InputStream to String
