流的关闭方式!
流务必要在finally里关闭。都是教训啊!
示例:
public static boolean file_isPass(String filePath, String fileType) {
File file = new File(filePath);
byte[] b = new byte[50];
InputStream is = null;
try {
is = new FileInputStream(file);
is.read(b);
//如果文件头和获取的map里 这些文件头一样,就为真实文件
String fileHead = FILE_TYPE_MAP.get(fileType);
int headLength = fileHead.length();
String fileTypeAllValue = getFileHexString(b);
if (fileTypeAllValue != null) {
// 获取的文件头转换为大写 进行比较
String fileTypeValue = fileTypeAllValue.substring(0, headLength).toUpperCase();
System.out.println("当前文件头:" + fileTypeValue);
if (fileHead.equals(fileTypeValue)) {
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return false;
}
当时没有在finally里关闭,导致成功直接return true。文件一直被占用。
务必要在finally里关闭流!
务必要在finally里关闭流!!
务必要在finally里关闭流!!!
本文来自博客园,作者:xiaolifc,转载请注明原文链接:https://www.cnblogs.com/xiaolibiji/p/16900296.html
浙公网安备 33010602011771号