Learn Java by Example: Java Program to Convert File to byte array and Vice-Versa

Hits: 2

Java Program to Convert File to byte array and Vice-Versa

In this program, you’ll learn to convert a File object to byte[] and vice-versa in Java.

 


Before we convert a file to byte array and vice-versa, we assume we have a file named test.txt in our src folder.

Here’s the content of test.txt

This is a
Test file.

Example 1: Convert File to byte[]


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) {

        }
    }
}

v

[84, 104, 105, 115, 32, 105, 115, 32, 97, 13, 10, 84, 101, 115, 116, 32, 102, 105, 108, 101, 46]

In the above program, we store the path to the file in the variable path.

Then, inside the try block, we read all the bytes from the given path using readAllBytes() method.

Then, we use Arrays‘ toString() method to print the byte array.

Since, readAllBytes() might throw an IOException, we’ve used the try-catch block in the program.


Example 2: Convert byte[] to File


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ByteFile{

    public static void main(String[] args){

        String path = System.getProperty("user.dir") + "\src\test.txt";
        String finalPath = System.getProperty("user.dir") + "\src\final.txt";

        try {
            byte[] encoded = Files.readAllBytes(Paths.get(path));
            Files.write(Paths.get(finalPath), encoded);
        } catch (IOException e) {

        }
    }
}

When you run the program, the contents of test.txt is copied to final.txt.

In the above program, we’ve used the same method as Example 1 to read all the bytes from the File stored in path. These bytes are stored in the array encoded.

We also have a finalPath where the bytes are to be written.

Then, we simply use the Files‘ write() method to write the encoded byte array to a File in the given finalPath.

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.