Here's one way to solve this without relying on third-party libraries:

inputStream.reset();
byte[] bytes = new byte[inputStream.available()];
DataInputStream dataInputStream = new DataInputStream(inputStream);
dataInputStream.readFully(bytes);

Or if you don't mind using thirdparties (Commons IO):


byte[] bytes = IOUtils.toByteArray(is);

Guava also helps:

byte[] bytes = ByteStreams.toByteArray(inputStream);
Answer from Mark Bramnik on Stack Overflow
🌐
GitHub
github.com › AdoptOpenJDK › openjdk-jdk11 › blob › master › test › jdk › java › io › InputStream › ReadAllBytes.java
openjdk-jdk11/test/jdk/java/io/InputStream/ReadAllBytes.java at master · AdoptOpenJDK/openjdk-jdk11
import java.util.Random; import jdk.test.lib.RandomFactory; · /* * @test · * @bug 8080835 8193832 · * @library /test/lib · * @build jdk.test.lib.RandomFactory · * @run main ReadAllBytes · * @summary Basic test for InputStream.readAllBytes · * @key randomness ·
Author   AdoptOpenJDK
🌐
Java Guides
javaguides.net › 2023 › 09 › java-files-readallbytes.html
Java Files readAllBytes()
September 27, 2023 - In this example, Files.readAllBytes() is used to read all the bytes from the file sample.txt. The bytes are then converted into a String for display. The method provides a quick way to read the entire content of a file without having to loop ...
Discussions

Support ByteStreams.toByteArray -> inputStream.readAllBytes()
Instead of Guava's ByteStreams#toByteArray(), InputStream.html#readAllBytes should be used. The latter is available since Java 9. More on github.com
🌐 github.com
9
January 12, 2024
There are so many Reader classes. Which one should I use to read from a file?
There are 2 axes to think about: What am I reading? Binary or text? If I'm reading binary, use an InputStream. If I'm reading text, use a Reader. A Reader basically wraps an InputStream and handles the conversion from whatever encoding was used to store the text (e.g., UTF-8, ISO-8859-1, etc.) What do I have? If you have a File, use a FileInputStream or FileReader. If you already have an InputStream, use an InputStreamReader. The other thing is that these things "stack". So if you have a File with some text, probably the best thing to do is to create a new BufferedReader(new FileReader(file)) BufferedReader is nice because you can read one line at a time using readLine(). BUT, there's a glitch here, which is that doing it that way doesn't set you specify how text in the file was actually encoded. So what you really need to do is: new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8")) Hope that helps. More on reddit.com
🌐 r/java
14
30
September 2, 2012
🌐
Attacomsian
attacomsian.com › blog › java-files-readallbytes-example
How to read a file using Files.readAllBytes() in Java
December 11, 2019 - try { // read all bytes byte[] bytes = Files.readAllBytes(Paths.get("input.txt")); // convert bytes to string String content = new String(bytes); // print contents System.out.println(content); } catch (IOException ex) { ex.printStackTrace(); ...
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
How to do in Java
howtodoinjava.com › home › i/o › read file to byte[] in java
Read File to Byte[] in Java
December 14, 2022 - In Java, reading a file to byte array may be needed in various situations. For example, passing the information through the network and other APIs for further processing. Let’s learn about a few ways of reading data from files into a byte array in Java. The Files.readAllBytes() is the best method for using Java 7, 8 and above.
Find elsewhere
🌐
GitHub
github.com › openrewrite › rewrite-migrate-java › issues › 390
Support ByteStreams.toByteArray -> inputStream.readAllBytes() · Issue #390 · openrewrite/rewrite-migrate-java
January 12, 2024 - Instead of Guava's ByteStreams#toByteArray(), InputStream.html#readAllBytes should be used. The latter is available since Java 9. - String inputStr = new String(ByteStreams. toByteArray(inputStream), Charsets.UJTF_8); + String inputStr = new ...
Author   openrewrite
🌐
Java Tips
javatips.net › api › java.nio.file.files.readallbytes
Java Examples for java.nio.file.Files.readAllBytes - Javatips.net
public static void main(String[] args) throws Exception { // get reference of source/input file java.nio.file.Path path = java.nio.file.Paths.get("input.pdf"); // read all the contents from source file into ByteArray byte[] data = java.nio.file.Files.readAllBytes(path); // create an instance of Stream object from ByteArray contents InputStream is = new ByteArrayInputStream(data); // Instantiate Document object from stream instance Document pdfDocument = new Document(is); // setup new file to be added as attachment FileSpecification fileSpecification = new FileSpecification("test.txt", "Sample text file"); // Specify Encoding property setting it to FileEncoding.None fileSpecification.setEncoding(FileEncoding.None); // add attachment to document's attachment collection pdfDocument.getEmbeddedFiles().add(fileSpecification); // save new output pdfDocument.save("output.pdf"); }
🌐
GitHub
github.com › bbossola › vulnerability-java-samples › blob › master › exploit › Encoder.java
vulnerability-java-samples/exploit/Encoder.java at master · bbossola/vulnerability-java-samples
import java.util.Base64; · public class Encoder { · public static void main(String[] args) throws Exception { byte[] classBytes = Files.readAllBytes(new File("Exploit.class").toPath()); byte[] encodedBytes = Base64.getEncoder().encode(classBytes); System.out.println(new String(encodedBytes)); } }
Author   bbossola
🌐
Medium
medium.com › @alxkm › how-to-read-a-file-into-a-string-in-java-multiple-approaches-6c781a1c3cbd
Java Interview: How to Read a File into a String in Java-Multiple Approaches | by Alex Klimenko | Medium
August 9, 2025 - For modern applications, prefer Files.readString() or Files.readAllBytes(); for external libraries, Apache Commons IO is a great fit. ... Exploring Java backend, algorithms, ML, and continuous learning through architecture best practices. https://github.com/alxkm
🌐
CalliCoder
callicoder.com › java-read-file
How to read a File in Java | CalliCoder
February 18, 2022 - import com.sun.org.apache.xpath.internal.operations.String; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class FilesReadAllBytesExample { public static void main(String[] args) { try { byte[] data = Files.readAllBytes(Paths.get("demo.txt")); // Use byte data } catch (IOException ex) { System.out.format("I/O error: %s%n", ex); } } } Facebook · Twitter · Linkedin · Reddit · Twitter · Github ·
🌐
How to do in Java
howtodoinjava.com › home › i/o › java read file to string (with examples)
Java Read File to String (with Examples)
October 22, 2023 - Path filePath = Path.of("c:/temp/demo.txt"); String fileContent = ""; try { byte[] bytes = Files.readAllBytes(Paths.get(filePath)); fileContent = new String (bytes); } catch (IOException e) { //handle exception } If you are still not using Java ...
🌐
DEV Community
dev.to › tomaszs2 › java-files-readallbytes-4m6i
Java: Files.readAllBytes() - DEV Community
January 24, 2021 - Files.readAllBytes() reads all the bytes from a file #java. Check out STJ: Java fantasy card game at... Tagged with java.
🌐
Mkyong
mkyong.com › home › java › java – how to convert file to byte[]
Java - How to convert File to byte[] - Mkyong.com
September 17, 2020 - package com.mkyong.io.howto; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FileToBytes { public static void main(String[] args) { try { String filePath = "/home/mkyong/test/phone.png"; // file to bytes[] byte[] bytes = Files.readAllBytes(Paths.get(filePath)); // bytes[] to file Path path = Paths.get("/home/mkyong/test/phone2.png"); Files.write(path, bytes); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } } $ git clone https://github.com/mkyong/core-java ·
🌐
iO Flood
ioflood.com › blog › read-file-java
Read Files in Java: Guide to File and Reader Classes
February 20, 2024 - Complexity: While BufferedReader is more efficient, it’s also more complex to set up and handle than readAllBytes(). Scanner is another class in Java used for parsing text for primitive types and strings using regular expressions. Here’s an example of how to use it:
🌐
Linux Hint
linuxhint.com › java-input-stream-read-all-bytes
Linux Hint – Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use