在Java中,我与在WinXP下运行的代码创建这样一个文件的工作:无法访问Java创建的文件 - 有时
public synchronized void store(Properties props, byte[] data) {
try {
File file = filenameBasedOnProperties(props);
if (file.exists()) {
return;
}
File temp = File.createTempFile("tempfile", null);
FileOutputStream out = new FileOutputStream(temp);
out.write(data);
out.flush();
out.close();
file.getParentFile().mkdirs();
temp.renameTo(file);
}
catch (IOException ex) {
// Complain and whine and stuff
}
}
有时候,当创建一个文件这种方式,它是几乎完全无法从代码之外访问(尽管负责打开和读取文件的代码没有问题),即使应用程序没有运行。当通过Windows资源管理器访问时,我无法移动,重命名,删除甚至打开文件。在Cygwin的,我得到了下面的时候我ls -l
目录:
ls: cannot access [big-honkin-filename]
total 0
?????????? ? ? ? ? ? [big-honkin-filename]
所暗示,文件名都大了,但在260个字符的最大值为XP(尽管它们是略微超过200个字符)。
为了进一步增加我的电脑只是想让我感到愚蠢的感觉,有时这个代码创建的文件是完全正常的。我发现的唯一模式是,一旦目录中的一个文件“锁定”,剩下的就被拧紧了。
有人曾经遇到过这样的事情,或者对这里发生的事情有什么见解?
(OT)当您即将关闭文件时,不需要刷新 - close()会执行所有需要的刷新。 – 2010-04-13 15:32:05
你应该检查mkdirs的返回值和renameTo – LB40 2010-04-13 15:39:58