From JDK 7 you can use Files.readAllBytes(Path).
Example:
Copyimport java.io.File;
import java.nio.file.Files;
File file;
// ...(file is initialised)...
byte[] fileContent = Files.readAllBytes(file.toPath());
Top answer 1 of 16
1517
From JDK 7 you can use Files.readAllBytes(Path).
Example:
Copyimport java.io.File;
import java.nio.file.Files;
File file;
// ...(file is initialised)...
byte[] fileContent = Files.readAllBytes(file.toPath());
2 of 16
584
It depends on what best means for you. Productivity wise, don't reinvent the wheel and use Apache Commons. Which is here FileUtils.readFileToByteArray(File input).
Videos
03:32
Java Program to Convert File to Byte Array - YouTube
03:53
How to Read From a File Using Java (Simple) - YouTube
10:17
Java Tutorial - File to Byte array - YouTube
03:52
java read file into byte array - YouTube
03:28
Java Program to Convert Byte Array to File | Copy One file to Another ...
Java Code Geeks
javacodegeeks.com › home › core java
7 Examples to Read File into a byte array in Java - Java Code Geeks
April 27, 2020 - Unfortunately, Java’s File class, which is used to represent both files and directories, doesn’t have a method say toByteArray(). It only holds path and allows you to perform certain operations like opening and closing file, but doesn’t allow you to directly convert File to a byte array. Anyway, no need to worry as there are several other ways to read File into a byte array and you will learn those in this Java file tutorial.
Vultr Docs
docs.vultr.com › java › examples › convert-file-to-byte-array-and-vice-versa
Java Program to Convert File to byte array and Vice-Versa | Vultr Docs
December 5, 2024 - This method makes use of the NIO package to read all bytes from the file at once. This is generally a cleaner and more efficient way to handle file data conversion to byte arrays. Once a file is converted to a byte array, you might need to convert it back to a file format. Here's how to accomplish this using Java...
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › io › fileinputstream
Read file in byte array with FileInputStream - Java Code Geeks
October 26, 2013 - Use read(byte[] b) API method of FileInputStream to read up to certain bytes of data from this input stream into the byte array. Create a String from the byte array. Don’t forget to close the FileInputStream, using the close() API method.
Mkyong
mkyong.com › home › java › java – how to convert file to byte[]
Java - How to convert File to byte[] - Mkyong.com
September 17, 2020 - In Java, we can use `Files.readAllBytes(path)` to convert a `File` object into a `byte[]`.
Coderanch
coderanch.com › t › 276442 › java › Reading-file-byte-array
Reading file into byte array (I/O and Streams forum at Coderanch)
December 12, 2003 - But what should happen is, that, it enters into another loop if the file is not read until the end, what is not done here, but that what's described. The method read returns integer, quote from javadoc: Reads up to len bytes of data from the input stream into an array of bytes.
Netjstech
netjstech.com › 2015 › 11 › how-to-convert-file-to-byte-array-java.html
Java Program to Convert a File to Byte Array | Tech Tutorials
You can use java.io.FileInputStream to read file content into a byte array using the read() method.
Blogger
javarevisited.blogspot.com › 2020 › 04 › 7-examples-to-read-file-into-byte-array-in-java.html
7 Examples to Read File into a Byte Array in Java
July 28, 2021 - Unfortunately, Java's File class, which is used to represent both files and directories, doesn't have a method say toByteArray(). It only holds path and allows you to perform certain operations like opening and closing file, but doesn't allow you to directly convert File to a byte array. Anyway, no need to worry as there are several other ways to read File into a byte array and you will learn those in this Java file tutorial.
Coderanch
coderanch.com › t › 277312 › java › read-file-data-byte-array
how to read a file data into byte array (I/O and Streams forum at Coderanch)
This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() ... Hi Right now I am using the following code to read the data from file into a StringBuffer.
Level Up Lunch
leveluplunch.com › java › examples › convert-file-to-byte-array
File to byte array | Level Up Lunch
October 28, 2013 - @Test public void file_to_byte_array_java () throws IOException { File file = new File(fileLocation); byte[] fileInBytes = new byte[(int) file.length()]; InputStream inputStream = null; try { inputStream = new FileInputStream(file); inputStream.read(fileInBytes); } finally { inputStream.close(); } assertEquals(18, fileInBytes.length); }
Programiz
programiz.com › java-programming › examples › convert-file-byte-array
Java Program to Convert File to byte array and Vice-Versa
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; public class FileByte { public static void main(String[] args) { String path = System.getProperty("user.dir") + "\\src\\test.txt"; try { byte[] encoded = Files.readAllBytes(Paths.get(path)); System.out.println(Arrays.toString(encoded)); } catch (IOException e) { } } }
Top answer 1 of 2
29
In Java 7 you can use the readAllBytes() method of Files class. See below:
Path fileLocation = Paths.get("C:\\test_java\\file.txt");
byte[] data = Files.readAllBytes(fileLocation);
There are many other ways to do it see here and here
2 of 2
1
This code worked for me, in an android project so hopefully, it will work for you.
private byte[] getByte(String path) {
byte[] getBytes = {};
try {
File file = new File(path);
getBytes = new byte[(int) file.length()];
InputStream is = new FileInputStream(file);
is.read(getBytes);
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return getBytes;
}
YouTube
youtube.com › watch
How to read a file in byte array in Java? - YouTube
In this video tutorial you will learn How to read a file in byte array in Java? In our example we are reading the text file into byte and also printing the d...
Published January 4, 2016