java删除文件怎么撤销,使用delete()删除文件 - Java

My code makes use of BufferedReader to read from a file [main.txt] and PrintWriter to write to a another temp [main.temp] file. I close both the streams and yet I was not able to call delete() method on the File object associated with [main.txt]. Only after calling System.gc() after closing both the stream was I able to delete the File object.

public static boolean delete (String str1, String str2, File FileLoc)

{

File tempFile = null;

BufferedReader Reader = null;

PrintWriter Writer = null;

try

{

tempFile = new File (FileLoc.getAbsolutePath() + ".tmp");

Reader = new BufferedReader(new FileReader(FileLoc));

Writer = new PrintWriter(new FileWriter(tempFile));

String lsCurrLine = null;

while((lsCurrLine = Reader.readLine()) != null)

{

// ...

// ...

if (true)

{

Writer.println(lsCurrLine);

Writer.flush();

}

}

Reader.close();

Writer.close();

System.gc();

}

catch(FileNotFoundException loFileExp)

{

System.out.println("\n File not found . Exiting");

return false;

}

catch(IOException loFileExp)

{

System.out.println("\n IO Exception while deleting the record. Exiting");

return false;

}

}

Is this reliable? Or is there a better fix?

解决方案

@user183717 - that code you posted is clearly not all of the relevant code. For instance, those "..."'s and the fact that File.delete() is not actually called in that code.

When a stream object is garbage collected, its finalizer closes the underlying file descriptor. So, the fact that the delete only works when you added the System.gc() call is strong evidence that your code is somehow failing to close some stream for the file. It may well be a different stream object to the one that is opened in the code that you posted.

Properly written stream handling code uses a finally block to make sure that streams get closed no matter what. For example:

Reader reader = new BufferedReader(new FileReader(file));

try {

// do stuff

} finally {

try {

reader.close();

} catch (IOException ex) {

// ...

}

}

If you don't follow that pattern or something similar, there's a good chance that there are scenarios where streams don't always get closed. In your code for example, if one of the read or write calls threw an exception you'd skip past the statements that closed the streams.

Is this [i.e. calling System.gc();] reliable?

No.

The JVM may be configured to ignore your application's gc() call.

There's no guarantee that the lost stream will be unreachable ... yet.

There's no guarantee that calling System.gc() will notice that the stream is unreachable. Hypothetically, the stream object might be tenured, and calling System.gc() might only collect the Eden space.

Even if the stream is found to be unreachable by the GC, there's no guarantee that the GC will run the finalizer immediately. Hypothetically, running the finalizers can be deferred ... indefinitely.

Or is there a better fix ?

Yes. Fix your application to close its streams properly.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值