How to convert File into Array of Bytes, and Array of Bytes to File in Java

Published on March 22, 2016 by Daniel Lanza

 java

Array of bytes will be calculated from a file, and then, that array of bytes will be used to create a file with different filename.

Array of bytes Java class

package com.dalanz.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ArrayBytesFile {

    // Attributes

    private File file;
    private byte[] bFile;

    // Constructors

    public ArrayBytesFile() {
        this.file = null;
        this.bFile = null;
    }

    public ArrayBytesFile (File file) {
        this.file = file;
        fileToBFile();
    }

    public ArrayBytesFile (String filePath, byte[] bFile) {
        this.file = new File(filePath);
        this.bFile = bFile;
    }

    // Public methods

    public File getFile() {
        return this.file;
    }

    public byte[] getBFile() {
        return this.bFile;
    }

    public void setFile (File file) {
        this.file = file;
    }

    public void setBfile (byte[] bFile) {
        this.bFile = bFile;
    }

    public void writeFile() {

        FileOutputStream outputStream = null;

        try {
            outputStream = new FileOutputStream(this.file);
            outputStream.write(this.bFile);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Private methods

    private void fileToBFile() {
        this.bFile = new byte[(int) this.file.length()];

        FileInputStream inputStream = null;

        try {
            inputStream = new FileInputStream(this.file);
            inputStream.read(bFile);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Main class

package test.com.dalanz;

import java.io.File;

import com.dalanz.file.ArrayBytesFile;

public class test {

    public static void main(String[] args) {

        String filePath = "/Users/dlanza/Desktop/Test.txt";
        File fileInput = new File (filePath);

        String filePathOut = "/Users/dlanza/Desktop/Test_out.txt";

        ArrayBytesFile file = new ArrayBytesFile(fileInput);
        byte[] bFile = file.getBFile();

        System.out.println("File: " + filePath);
        System.out.println("Array of bites: " + bFile.toString());
        System.out.println("--------------------------");

        ArrayBytesFile fileOutput = new ArrayBytesFile(filePathOut, bFile);
        System.out.println("File: " + filePathOut);
        fileOutput.writeFile();
        System.out.println("File created");
    }
}

Output

File: /Users/dlanza/Desktop/Test.txt
Array of bites: [B@6d06d69c
--------------------------
File: /Users/dlanza/Desktop/Test_out.txt
File created
comments powered by Disqus

See also

How to get MD5 checksum for a file in Java

MD5 checksum is a 128-bit hash value (32 digit hexadecimal number). This works as a fingerprint for a file which let us compare together other files to find out duplicate files. These Java classes will let us calculate MD5 checksum for any single file. Maven dependencies <dependencies> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies> MD5 Java class package com.dalanz.file; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.

Read more

How to set JAVA_HOME on Mac OS X

If you are planing to develop Java Apps on your Mac, you may have to set $JAVA_HOME environment variable. Install the latest Java Virtual Machine Go to How to install JDK on Mac OS X) to install a new Java Virtual Machine. Download the latest Java JDK package from Oracle. Check Java Virtual Machines List directories in the following root -> /Library/Java/JavaVirtualMachines $ ls -l /Library/Java/JavaVirtualMachines drwxr-xr-x 3 root wheel 102 23 dic 20:36 jdk1.

Read more

How to resolve 404 error with Tomcat Server and Eclipse

If you start Tomcat from Eclipse and get a 404-Error code when you go to http://localhost:8080, there is a easy way to fix the issue. Just go to Properties for Tomcat (right click on Tomcat - Servers Tab) Switch location Tomcat Properties Switch location from [workspace metadata] to [/Servers/Tomcat v…] Restart Tomcat from Eclipse and go back to http://localhost:8080. You will see Tomcat configuration page. Tomcat configuration page

Read more