Here you can find the source of fileToByteArray(String fname)
public static byte[] fileToByteArray(String fname)
//package com.java2s; /******************************************************************************* * Copyright ? 2012-2015 eBay Software Foundation * This program is dual licensed under the MIT and Apache 2.0 licenses. * Please see LICENSE for more information. *******************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Main { public static byte[] fileToByteArray(String fname) { File file = new File(fname); FileInputStream fis = null; try {/*from w w w.ja v a2 s. c o m*/ fis = new FileInputStream(fname); } catch (FileNotFoundException e) { return null; } byte[] fileContents = new byte[(int) file.length()]; int i = 0; while (true) { int data; try { data = fis.read(); } catch (IOException e) { return null; } finally { if (fis != null) try { fis.close(); } catch (IOException e) { //ignore } } if (data == -1) break; fileContents[i] = (byte) data; } return fileContents; } }