Here you can find the source of readFile(File file)
Parameter | Description |
---|---|
file | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static byte[] readFile(File file) throws IOException
//package com.java2s; /*//from ww w. ja v a 2s . c o m * @(#)FileUtil.java * * Copyright (C) 2006-2008 www.interpss.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE * as published by the Free Software Foundation; either version 2.1 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * @Author Mike Zhou * @Version 1.0 * @Date 01/30/2008 * * Revision History * ================ * */ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Main { /** * read a file and return file context as a byte[] * * @param file * @return * @throws IOException */ public static byte[] readFile(File file) throws IOException { InputStream inStream = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int n; byte[] buf = new byte[4096]; do { n = inStream.read(buf); if (n >= 0) bos.write(buf, 0, n); } while (n > 0); inStream.close(); return bos.toByteArray(); } }