简体   繁体   English

尝试在Java中重命名文件不起作用(使用java.io.File.renameTo方法)

[英]Attempting to rename a File in Java does not work (using java.io.File.renameTo method)

I have a method where I load a file, do some changes, and then save it again. 我有一个加载文件,进行一些更改然后再次保存的方法。 However, as insurance against exceptions during saving I create a new File and save to that one first (since improperly formated data can cause an Exception in the middle of saving). 但是,为了防止保存期间发生异常,我创建了一个新文件并首先保存到该文件(因为格式不正确的数据可能会在保存过程中导致异常)。 Once the saving is complete I delete the original file and rename the new one to have the original's name. 保存完成后,我将删除原始文件,然后将新文件重命名为具有原始文件的名称。

Code looks essentially like this: 代码本质上是这样的:

MyDataClass.save(tempfile);
originalfile.delete();
tempfile.renameTo(originalfile);

The issue is that the call to the renameTo method always returns 'false' and the new file (tempfile) remains with the random name it was created with (the original file is deleted). 问题在于,对namedTo方法的调用始终返回“ false”,并且新文件(临时文件)将保留其创建时使用的随机名称(原始文件将删除)。

Anyone have a guess as to why the renaming fails? 任何人都怀疑为什么重命名失败?

I guess this may be because of the program unable to check the recent status of the file deletion. 我猜这可能是由于程序无法检查文件删除的最新状态。 You can debug this by trying out the following code: 您可以通过尝试以下代码来调试它:

MyDataClass.save(tempfile);
if(originalfile.delete()==true){
tempfile.renameTo(originalfile);
}
else{
System.out.println("File is not deleted");
}

Or you can try this. 或者您可以尝试一下。

MyDataClass.save(tempfile);
originalfile.delete();
Thread.sleep(4000);//Make the thread sleep so that the recent status can be detected
tempfile.renameTo(originalfile);

Also, I see that you have used MyDataClass.save to save the file. 另外,我看到您已经使用MyDataClass.save保存文件。 Please check if you closed the file after saving. 保存后请检查是否关闭了文件。

If the file is locked when you tried to rename, it might not have worked 如果您在尝试重命名时文件被锁定,则可能无法正常工作

Also, if you are using windows explorer to view your files, you might want to refresh to see the recent file created 另外,如果您使用Windows资源管理器查看文件,则可能需要刷新以查看最近创建的文件

See Files.move for the newer classes Path, Paths, and Files. 请参阅Files.move以获取更新的类Path,Paths和Files。

MyDataClass.save(tempfile);
Files.move(tempfile.toPath(), originalfile.toPath(),
    StandardCopyOption.REPLACE_EXISTING,
    StandardCopyOption.ATOMIC_MOVE);

This of course assumes that save closed the tempfile correctly. 当然,这假定save关闭了tempfile。

I have finally found out the issue. 我终于找到了问题所在。 The thing is, I was using the Apache POI (Microsoft Excel Library) and it wouldn't save updates like it should. 事实是,我使用的是Apache POI(Microsoft Excel库),它不会像应有的那样保存更新。 Trying to save to the existing file caused exceptions, so I thought I'd save to a second file and then delete the original/rename the new one. 尝试保存到现有文件会导致异常,因此我想先保存到第二个文件,然后删除原始文件/重命名新文件。 Turns out, however, that the very act of saving it to a new file also causes it to save to the original file!! 事实证明,将其保存到新文件的行为也导致它保存到原始文件中!! So I just have to save to a temporary file and then delete it... 所以我只需要保存到一个临时文件然后删除它...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM