Java File to Byte Array fileToBytes(File file)

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

Description

file To Bytes

License

Apache License

Declaration

public static byte[] fileToBytes(File file) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Main {
    public static byte[] fileToBytes(File file) {

        FileInputStream fin = null;
        byte arr[] = new byte[(int) file.length()];
        try {/*  w  ww . j  a v  a2 s .  c o m*/
            fin = new FileInputStream(file);
            fin.read(arr);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            close(fin);
        }

        return arr;
    }

    public static void close(Closeable... closes) {
        //        RuntimeException re = null;
        if (closes == null) {
            return;
        }
        for (Closeable close : closes) {
            try {
                if (close != null) {
                    close.close();
                }
            } catch (IOException e) {
                //                re = new RuntimeException(e.getMessage(), e);
            }
        }
    }
}

Related

  1. fileToByteArray(final File f)
  2. fileToByteArray(final IFile file)
  3. fileToByteArray(String filePath)
  4. fileToByteArray(String fname)
  5. fileToByteArray(String path)
  6. fileToBytes(File file)
  7. fileToBytes(File path)
  8. fileToBytes(File source)
  9. fileToBytes(final File f)