Tuesday, June 28, 2016

Java Convert File Into Bytes (Array of Bytes)


package com.pritom.kumar;

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

/**
 * Created by pritom on 28/06/2016.
 */
public class ReadFileAsByteArray {
    public static void main (String[] args) throws Exception {
        File file = new File("x1.zip");

        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] toByte = new byte[(int) file.length()];
        fileInputStream.read(toByte);
        fileInputStream.close();

        System.out.println("File_Length=" + file.length() + ", Byte_Length=" + toByte.length + ", Both_Are_Equal=" + (file.length() == toByte.length ? "True" : "False"));
    }
}


Output:

File_Length=14805774, Byte_Length=14805774, Both_Are_Equal=True

No comments:

Post a Comment