标签:
// 读取文件路径转换成byte[]类型
private static byte[] getBytes(String str) throws IOException {
byte[] bytes = null;
if (str != null || !str.equals("")) {
File file = new File(str);
InputStream is = new FileInputStream(file);
int length = (int) file.length();
if (length > Integer.MAX_VALUE) // 当文件的长度超过了int的最大值
{
System.out.println("路径过长!!");
return null;
}
bytes = new byte[length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// 如果得到的字节长度和file实际的长度不一致就可能出错了
if (offset < bytes.length) {
System.out.println("字符长度图实际长度不一致");
return null;
}
is.close();
}
return bytes;
}
标签:
原文地址:http://www.cnblogs.com/lzw0414/p/5411531.html