Сравните, используя File.lastmodified в java

Мне нужно сравнить последние измененные метки времени из двух мест. Если они не совпадают, мне нужно скопировать файл из первого места во второе, а также установить измененную метку времени второго с отметкой первого.

Я пробую это с помощью File.lastModified в java. Но я получаю разные значения 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;
} 
7
задан Ishita 13 December 2011 в 11:17
поделиться