public static String readFile(File file) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); BufferedInputStream is = new BufferedInputStream(new FileInputStream(file)); try { IOUtils.copyBytes(is, os, 1024, true); } finally { is.close(); } return os.toString(); }
/** * Copies from one stream to another. * * @param in * InputStrem to read from * @param out * OutputStream to write to * @param buffSize * the size of the buffer * @param close * whether or not close the InputStream and OutputStream at the * end. The streams are closed in the finally clause. */ public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException { try { copyBytes(in, out, buffSize); if (close) { out.close(); out = null; in.close(); in = null; } } finally { if (close) { closeStream(out); closeStream(in); } } }
/** * Copies from one stream to another. * * @param in * InputStrem to read from * @param out * OutputStream to write to * @param buffSize * the size of the buffer * @param close * whether or not close the InputStream and OutputStream at the * end. The streams are closed in the finally clause. */ public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException { try { copyBytes(in, out, buffSize); if (close) { out.close(); out = null; in.close(); in = null; } } finally { if (close) { closeStream(out); closeStream(in); } } }