在应用程序退出之前,RandomAccessFiles不会关闭 [英] RandomAccessFiles don't close until application exit

查看:422
本文介绍了在应用程序退出之前,RandomAccessFiles不会关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用随机存取文件的项目中工作.我遇到的最大问题是,即使我在访问文件后关闭文件,该文件也要等到整个应用程序退出后才能关闭.这是标准行为,还是有人知道发生了什么?该代码基本上看起来像:

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();

我正在使用RandomAccessFile的两个部分都是这样的(即,我100%确信我正在关闭文件).

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.)

推荐答案

您要确保您的关闭位于这样的finally块内

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();
   }
}

否则,异常可能导致close()永远不会执行.

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

这篇关于在应用程序退出之前,RandomAccessFiles不会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆