我今天遇到了一个我们的实用工具类的问题.它是文件的助手,包含一些静态文件复制例程.以下是与测试方法一起提取的相关方法.
问题是有时setLastModified调用失败,返回false.
在我的PC上(Windows 7,最新的Java),我有时会收到"setLastModified failed"消息(大约25次中的1000次).
我现在通过删除FileChannel.close调用解决了这个问题,但我更愿意理解为什么会发生这种情况,即使这是正确的解决方案.
有没有其他人得到同样的问题?
private void testCopy() throws FileNotFoundException, IOException {
File src = new File("C:\\Public\\Test-Src.txt");
File dst = new File("C:\\Public\\Test-Dst.txt");
for (int i = 0; i < 1000; i++) {
copyFile(src, dst);
}
}
public static void copyFile(final File from, final File to) throws FileNotFoundException, IOException {
final String tmpName = to.getAbsolutePath() + ".tmp";
// Copy to a .tmp file.
final File tmp = new File(tmpName);
// Do the transfer.
transfer(from, tmp);
// Preserve time.
if …Run Code Online (Sandbox Code Playgroud)