比较Java中使用File.lastModified的方法

8
我需要比较两个位置的最后修改时间戳。如果它们不匹配,我需要将文件从第一个位置复制到第二个位置,并将第二个位置的修改时间戳设置为第一个位置的修改时间戳。
我尝试使用java中的File.lastModified来实现此目的。但是,即使未修改文件,我在不同时间得到了不同值的File.lastModified。请注意,我在Linux中进行此操作。
请问有人知道问题出在哪里吗?
谢谢
代码如下:
/**
 * Copies files from Source to Destination Directory
 *
 * @param src Source Directory
 * @param destFolder Destination Directory
 * @param existingROOTNames Existing ROOT files
 * @return boolean flag to indicate whether root context has changed
 */
private static boolean copyFiles(File src, File destFolder, String[] existingROOTNames) {
    final String[] fileNames = src.list();
    boolean changeRootContext = false;
    File srcFile = null;
    File destFile = null;
    List rootFileList = Arrays.asList(existingROOTNames);
    int rootFileIndex = -1;
    long srcFileTime;
    long destFileTime;
    for (int index = 0; index < fileNames.length; index++) {

        srcFile = new File(src, fileNames[index]);
        destFile = new File(destFolder.getPath(),fileNames[index]);

        if (srcFile.isFile()) {
            if (log.isEnabled(DEBUG)) {
                log.debug("copy file : " + srcFile);
            }
            srcFileTime = srcFile.lastModified();
            destFileTime = destFile.lastModified();
            if(hasFileChanged(srcFileTime,destFileTime)){
                changeRootContext = true;
                if (log.isEnabled(XDEBUG)) {
                    log.debug(XDEBUG,"changing flag to true for : " + srcFile);
                    log.debug(XDEBUG,"changing flag srcFile.lastModified() : " + srcFileTime);
                    log.debug(XDEBUG,"changing flag destFile.lastModified() : " + destFileTime);
            }
            }
            try {
                FileUtil.fastChannelCopy(srcFile.getPath(), destFolder.getPath());
                log.debug("changing flag while modifying destFile.lastModified() : " + destFile.setLastModified(srcFileTime));
                log.debug("changing flag after modifying destFile.lastModified() : " + destFile.lastModified());
                rootFileIndex = rootFileList.indexOf(fileNames[index]);
                if(rootFileIndex!=-1){
                    existingROOTNames[rootFileIndex]=null;
                }

            } catch (IOException e) {
                log.debug("unable to copy source file : "
                        + fileNames[index], e);
            }
        }
    }
    return changeRootContext;
}


/**
 * Checks whether the provided timestamp matches or not
 * This is required as in linux the time is approximated to nearest milliseconds
 *
 * @param srcFileTime
 * @param destFileTime
 * @return whether matched or not
 */
private static boolean hasFileChanged(long srcFileTime, long destFileTime){
    return Math.abs(srcFileTime-destFileTime) > 1000l;
} 

1
你可以打印出带有毫秒的时间戳来查看它们是否与 ls -l 中文件的时间戳匹配吗? - Peter Lawrey
在Linux中,ls -l选项以正确的日期格式显示,例如:-rw-rw-r-- 1 uccxuser uccxservice 3362 Dec 7 23:50 / opt / cisco / uccx / Documents / User / default / webapps / ROOT / cc.txt。我在程序中打印File.lastModified返回的长值,并看到它每次返回不同的值。一些例子:更改标志srcFile.lastModified():1323282046000更改标志srcFile.lastModified():1323772334000更改标志srcFile.lastModified():1323282046000 - Ishita
更改标志似乎会更改上次修改日期。您能在更改标志后重置上次修改日期吗? - Peter Lawrey
@Peter Lawrey,flag只是一个本地变量,如果我认为文件已被修改,则会将其设置。稍后它会用于其他一些目的。 - Ishita
1
FileUtil.fastChannelCopy() 函数是否会关闭底层流?您尝试过进行原始复制或使用其他库,如org.apache.commons.io.FileUtils吗? - Andrés Oviedo
显示剩余2条评论
1个回答

1

你能确定没有进程正在访问该文件吗? 尝试使用 fuser -k /path/to/your/filename 来终止可能正在访问测试文件的任何进程。


我尝试使用fuser -i命令,以便在需要时询问我是否要杀死进程。但是,运气不佳,这也没有起作用。[root@dactyl-esx-sa ~]# fuser-i /opt/cisco/uccx/Documents/User/default/webapps/ROOT/cc.txt [root@dactyl-esx-sa ~]# - Ishita

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接