RandomAccessFiles don't close until application exit

余生长醉 提交于 2019-12-25 00:15:25

问题


I'm working on a project where I'm using RandomAccessFile. The biggest issue I am having is that even though I close the file after it being accessed the file does not close until the entire application exits. Is this standard behavior or does anyone have some idea what's going on? The code basically looks like:

RandomAccessFile raf = new RandomAccessFile(f);
//do stuff
raf.close();

Both sections where I am using a RandomAccessFile are like this (i.e. I am 100% sure that I am calling close on the files.)


回答1:


You want to make sure that your close is inside a finally block like this

RandomAccesFile raf = null;
try {
    raf = new RandomAccessFile(f);
    //do stuff
} finally {
   if (raf != null) {
      raf.close();
   }
}

Otherwise an exception can cause close() never to be executed.



来源:https://stackoverflow.com/questions/1213374/randomaccessfiles-dont-close-until-application-exit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!