Here you can find the source of fileToByteArray(File file)
Parameter | Description |
---|---|
file | The file to convert. |
Parameter | Description |
---|---|
IOException | an exception |
public static byte[] fileToByteArray(File file) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class Main { /**//from ww w .j a v a 2s . c o m * Converts a file into an array of bytes. * * @param file The file to convert. * * @return The file in bytes. * * @throws IOException */ public static byte[] fileToByteArray(File file) throws IOException { final byte[] data = new byte[Math.toIntExact(file.length())]; try (FileInputStream fis = new FileInputStream(file)) { fis.read(data); } return data; } }