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

private static byte[] getBytesFromFile(String filepath) throws IOException 

Method Source Code


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

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

public class Main {
    private static byte[] getBytesFromFile(String filepath) throws IOException {
        File file = new File(filepath);
        InputStream is = new FileInputStream(file);
        long length = file.length();
        byte[] bytes = new byte[(int) length];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;//from w ww . java 2  s . c o m
        }
        is.close();
        return bytes;
    }
}

Related

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