Java Is File Newer isNewer(File file1, File file2)

Here you can find the source of isNewer(File file1, File file2)

Description

This will check to see if file1 is newer than file2.

License

Open Source License

Parameter

Parameter Description
file1 a parameter
file2 a parameter

Return

indication if file1 is newer than file2

Declaration

public static Boolean isNewer(File file1, File file2) 

Method Source Code

//package com.java2s;
/*/*from  w  ww.  j av  a 2  s  .  co m*/
 * RHQ Management Platform
 * Copyright (C) 2005-2008 Red Hat, Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation, and/or the GNU Lesser
 * General Public License, version 2.1, also as published by the Free
 * Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License and the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * and the GNU Lesser General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

import java.io.File;

public class Main {
    /**
     * This will check to see if file1 is newer than file2. If file1's last modified date
     * is <strong>after</strong> file2's last modified date, then <code>true</code> is returned.
     * <code>false</code> is returned if file1's date is the same or older than file2's date.
     * <p>
     * <code>null</code> is returned if any of these conditions are true:
     * <ul>
     * <li>If either file is null</li>
     * <li>If either file does not exist</li>
     * <li>If either file is not a normal file (but, say, a directory)</li>
     * </ul>
     * </p>
     *
     * @param file1
     * @param file2
     * @return indication if file1 is newer than file2
     */
    public static Boolean isNewer(File file1, File file2) {
        if (file1 == null || file2 == null) {
            return null;
        }
        if (!file1.isFile() || !file2.isFile()) {
            return null;
        }
        long file1Date = file1.lastModified();
        long file2Date = file2.lastModified();
        return file1Date > file2Date;
    }
}

Related

  1. isNewer(File file, File reference)
  2. isNewer(String file, String reference)
  3. isNewerOrEqual(String newFile, String oldFile)