In programmers life,We need to convert the files to byte array to sent over network.
There are different ways to convert file to byte array.
Table of Content :
- Introduction
- Read file to byte[] array with FileInputStream
- Read file to byte[] array with NIO
- Read file to byte array using common.io's FileUtils
- Read file to byte[] array using Google Guava
- Questions related to Read file to byte[] array in Java
- Summary
Read file to byte[] array with FileInputStream
package com.javacodestuffs.io.files.convert.tobytes;
import java.io.File;
import java.io.FileInputStream;
public class ReadFileAsByteArray1 {
public static void main(String[] args) {
File file = new File("src/main/resources/abc.txt"); // this file we kept in classpath
FileInputStream fileInputStream = null;
byte[] bytes = new byte[(int) file.length()];
try {
// convert file into byte array
fileInputStream = new FileInputStream(file);
fileInputStream.read(bytes);
fileInputStream.close();
for (int i = 0; i < bytes.length; i++) {
System.out.print((char) bytes[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
output:
Hello World!!!
Read file to byte[] array with NIO
package com.javacodestuffs.io.files.convert.tobytes;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ReadFileAsByteUsingNIO {
public static void main(String[] args) throws IOException {
{
Path path = Paths.get("src/main/resources/abc.txt");
byte[] data = Files.readAllBytes(path);
System.out.println(data);
}
}
}
Read file to byte array using common.io's FileUtils
Using Apache commons IO's FileUtils and IOUtils class, we can convert or read file to byte array
Maven dependency
we need to include maven dependency in our maven project.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
public class ReadFileAsByteUsingApacheCommons {
public static void main(String[] args) throws IOException {
{
// Using FileUtils.readFileToByteArray()
File file1 = new File("src/main/resources/abc1.txt");
File file2 = new File("src/main/resources/abc2.txt");
FileInputStream fileInputStream = new FileInputStream(file1);
byte[] bytes1 = FileUtils.readFileToByteArray(file2);
// Using IOUtils.toByteArray
byte[] bytes2 = IOUtils.toByteArray(fileInputStream);
System.out.println("printing the byte array content1:");
for (int i = 0; i < bytes1.length; i++) {
System.out.print((char) bytes1[i]);
}
System.out.println();
System.out.println("printing the byte array content2:");
for (int i = 0; i < bytes2.length; i++) {
System.out.print((char) bytes2[i]);
}
}
}
}
Read file to byte[] array using Google Guava
Another way to read files as byte array is use google Guava library.
Maven dependency
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
public class ReadFileAsByteUsingGuava {
public static void main(String[] args) throws IOException {
{
// Using FileUtils.readFileToByteArray()
File file1 = new File("src/main/resources/abc1.txt");
File file2 = new File("src/main/resources/abc2.txt");
//Using Files.toByteArray()
byte[] bytes1 = Files.toByteArray(file1);
//Using ByteStreams.toByteArray
FileInputStream fileInputStream = new FileInputStream(file2);
byte[] bytes2 = ByteStreams.toByteArray(fileInputStream);
......
.....
Post/Questions related to How to Read file to byte[] array in Java
How do you load properties files from the Resources folder in Java?
What is the Java Resources folder?
How do I read a file from the Resources folder in spring boot?
How do I read a resource folder?
How to read a file from src folder in java?
In this article, we have seen Read file to byte[] array in Java. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment