您的位置:首页 > 其它

将文件转成byte[]文件属组

2016-02-17 16:40 225 查看
/**
*
*  @Description    : 读取文件数组
*  @Method_Name    : fileBuff
*  @param filePath
*  @return
*  @throws IOException
*  @return         : byte[]
*  @Creation Date  : 2015年1月27日 下午5:26:49
*  @Author         :
*/
public static byte[] fileBuff(String filePath) throws IOException {
File file = new File(filePath);
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
//System.out.println("file too big...");
return null;
}
FileInputStream fi = new FileInputStream(file);
byte[] file_buff = new byte[(int) fileSize];
int offset = 0;
int numRead = 0;
while (offset < file_buff.length && (numRead = fi.read(file_buff, offset, file_buff.length - offset)) >= 0) {
offset += numRead;
}
// 确保所有数据均被读取
if (offset != file_buff.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
fi.close();
return file_buff;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: