所以,我有一個讀取文件數據的函數,在這種情況下,圖像大小。但完成後它似乎不能正確釋放這些文件。之後我無法移動這些文件。如果我不叫這個功能一切正常,但如果我做我總是得到「使用文件..等等等等等等」未在java中發佈的資源
private void setMoveType() {
ImageInputStream in = null;
try {
in = ImageIO.createImageInputStream(new FileInputStream(file.toString()));
try {
final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
if(readers.hasNext()) {
ImageReader reader = readers.next();
try {
reader.setInput(in);
try {
moveType = Helper.getMoveType(new Dimension(reader.getWidth(0), reader.getHeight(0)));
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
return;
}
} catch(Exception e) {
System.err.println("ReaderException: " + e.getMessage());
} finally {
reader.dispose();
}
}
} catch(Exception e) {
System.err.println("MoveTypeSetException: " + e.getMessage());
}
} catch (IOException e) {
System.err.print("IOException: failure while creating image input stream");
System.err.println(" -> createImageInputStream Error for file: " + file.getFileName());
return;
} finally {
if(in != null) {
try {
in.close();
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
return;
}
}
}
}
編輯:ImageInputStream中的不正常關閉
EDIT2:一個FileInputStream沒有關閉
用[嘗試 - 與資源(https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)語句。 – Tom
@湯姆也沒有運氣。 – IntoDEV