Here you can find the source of readFileToByteArray(File file)
public static byte[] readFileToByteArray(File file)
//License from project: Apache License import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Main{ private static final Logger logger = LoggerFactory .getLogger(FileUtil.class); public static byte[] readFileToByteArray(String filePath) { if (StringUtil.isEmpty(filePath)) { return null; }//from w w w. ja va 2 s .c o m File file = new File(filePath); return readFileToByteArray(file); } public static byte[] readFileToByteArray(File file) { byte[] fileBytes = new byte[(int) file.length()]; FileInputStream fis = null; try { fis = new FileInputStream(file); int offset = 0; int count = (int) file.length(); int temp = 0; while ((temp = fis.read(fileBytes, offset, count)) > 0) { offset += temp; count -= temp; } } catch (FileNotFoundException e) { logger.error("readFile", e); } catch (IOException e) { logger.error("readFile", e); } catch (Exception e) { logger.error("readFile", e); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { logger.error("readFile", e); } } } return fileBytes; } }