關閉 FileChannel 打開的文件

FileInputStream in = new FileInputStream(file);

FileChannel ch = in.getChannel();

MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,

file.length());

messageDigest.update(byteBuffer);

/*

* in.close(); in = null; byteBuffer.clear(); byteBuffer = null;

* ch.close(); ch = null;

*/

if(file.length() > 0){

Method getCleanerMethod;

sun.misc.Cleaner cleaner = null;

try {

getCleanerMethod = byteBuffer.getClass().getMethod("cleaner",

new Class[0]);

getCleanerMethod.setAccessible(true);

cleaner = (sun.misc.Cleaner) getCleanerMethod

.invoke(byteBuffer, new Object[0]);

cleaner.clean();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return bufferToHex(messageDigest.digest());


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章