简体   繁体   English

比较在java中使用File.lastmodified

[英]Compare using File.lastmodified in java

I need to compare the last modified time stamps from two locations. 我需要比较两个位置的最后修改时间戳。 If they dont match, I need to copy file from first location to second and also set the modified time stamp of second with that of the first. 如果它们不匹配,我需要将文件从第一个位置复制到第二个位置,并将第二个修改时间戳设置为第一个。

I am trying this with File.lastModified in java. 我正在尝试使用java中的File.lastModified。 But I am getting different values of File.lastModified for the same file at different times even though they are not being modified. 但是我在不同的时间为同一个文件获取不同的File.lastModified值,即使它们没有被修改。 Please note that am trying this in linux. 请注意我在linux中尝试这个。

Can anyone point whats going wrong ? 有人能指出什么是错的吗?

Thanks 谢谢

Copying the code : 复制代码:

/**
 * 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;
} 

Can you be sure that there are not processes accessing the file? 你能确定没有进程访问该文件吗? Try with fuser -k /path/to/your/filename to kill any process that could be accessing the test files. 尝试使用fuser -k / path / to / your / filename来杀死任何可能访问测试文件的进程。

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

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