Java tutorial
//package com.java2s; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** * Reads the given local file and returns the contents as a byte array. * * @param inputFileName the name of the file to read from * @return the file's contents as a byte array */ public static byte[] readFile(String inputFileName) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream in = new FileInputStream(inputFileName); try { copy(in, out); } finally { in.close(); } return out.toByteArray(); } private static void copy(InputStream in, OutputStream out) throws IOException { out = new BufferedOutputStream(out, 0x1000); in = new BufferedInputStream(in, 0x1000); // Copy the contents from the input stream to the output stream. while (true) { int b = in.read(); if (b == -1) { break; } out.write(b); } out.flush(); } }