开发者

Why can't my program delete the files it creates under Win7?

开发者 https://www.devze.com 2023-04-08 00:14 出处:网络
I\'ve made a Java program in Eclipse. I started on Windows XP, but have recently upgraded. As part of it\'s saving mechanism, the program writes the settings to a file settings_new.sav. If that goes

I've made a Java program in Eclipse. I started on Windows XP, but have recently upgraded.

As part of it's saving mechanism, the program writes the settings to a file settings_new.sav. If that goes ok, it deletes the settings.sav, and renames the new one to match the old name. While it worked under winXP (at least I thought it di开发者_JS百科d, but I can't check now), under win7, it fails to delete the file, even though it was the program that created it (although, a different instance of the program).

The file is picked up by Eclipse and can be deleted from there quite happily. I can delete it manually. I am the admin on my own computer. The folder is just inside the workspace folder, and is not in Program Files (though, I have no idea if eventual users will install it there). The program can create and modify files just fine. It's not throwing any Exception, which I thought it would if it was win7 blocking it.

Any ideas?


It is due to file-locking mechanism in java.Make sure you close the buffering streams such as BufferedReader, BufferedInputStream on that file when done.


I used to have this problem, when you are done using your file you have to set your file equal to null. So if you do something like:

public void createFile(String path)
{
    File file = new File(path);
    file.createNewFile();
    file = null
}

you have to set the file to null when you are done using it so that the system stops using that file. you have to do the same thing with FileReader and FileWriter. you have to set your file readers and your file writers to null in order to access the file again. Give this a try and let me know how it goes.

0

精彩评论

暂无评论...
验证码 换一张
取 消