Java File to Byte Array fileToByteArray(File file)

Here you can find the source of fileToByteArray(File file)

Description

Loads data from file and converts it to a byte array.

License

Open Source License

Parameter

Parameter Description
file the file to convert

Exception

Parameter Description

Return

a byte arry containing the converted data

Declaration

public static byte[] fileToByteArray(File file) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
import java.io.*;

public class Main {
    /**/*from ww w  .  java 2 s .  co m*/
     * Loads data from file and converts it to a byte array.
     *
     * @param file the file to convert
     * @return a byte arry containing the converted data
     * @throws java.io.FileNotFoundException
     * @throws java.io.IOException
     */
    public static byte[] fileToByteArray(File file) throws FileNotFoundException, IOException {
        byte[] result;
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            result = new byte[(int) file.length()];
            fileInputStream.read(result);
        }

        return result;
    }
}

Related

  1. fileToByteArray(File file)
  2. FiletoByteArray(File file)
  3. fileToByteArray(File file)
  4. fileToByteArray(File file)
  5. fileToByteArray(final File f)
  6. fileToByteArray(final IFile file)
  7. fileToByteArray(String filePath)