Java File to Byte Array getBytesFromFile(String filePath)

Here you can find the source of getBytesFromFile(String filePath)

Description

get Bytes From File

License

Apache License

Declaration

public static byte[] getBytesFromFile(String filePath) 

Method Source Code


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

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

public class Main {
    public static byte[] getBytesFromFile(String filePath) {
        return getBytesFromFile(new File(filePath));
    }/*from   w  w  w.j  a v  a  2  s . c  o  m*/

    public static byte[] getBytesFromFile(File f) {
        if (f == null) {
            return null;
        }
        try {
            FileInputStream stream = new FileInputStream(f);
            ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = stream.read(b)) != -1) {
                out.write(b, 0, n);
            }
            stream.close();
            out.close();
            return out.toByteArray();
        } catch (IOException e) {
        }
        return null;
    }
}

Related

  1. getBytesFromFile(String filename)
  2. getBytesFromFile(String filename)
  3. getBytesFromFile(String fileName)
  4. getBytesFromFile(String filename)
  5. getBytesFromFile(String filepath)
  6. toByteArray(File file)