Example usage for java.io File lastModified

List of usage examples for java.io File lastModified

Introduction

In this page you can find the example usage for java.io File lastModified.

Prototype

public long lastModified() 

Source Link

Document

Returns the time that the file denoted by this abstract pathname was last modified.

Usage

From source file:com.izforge.izpack.util.file.FileUtils.java

/**
 * Convenience method to copy a file from a source to a
 * destination specifying if token filtering must be used, if
 * filter chains must be used, if source files may overwrite
 * newer destination files and the last modified time of
 * <code>destFile</code> file should be made equal
 * to the last modified time of <code>sourceFile</code>.
 *
 * @param sourceFile           the file to copy from.
 *                             Must not be <code>null</code>.
 * @param destFile             the file to copy to.
 *                             Must not be <code>null</code>.
 * @param overwrite            Whether or not the destination file should be
 *                             overwritten if it already exists.
 * @param preserveLastModified Whether or not the last modified time of
 *                             the resulting file should be set to that
 *                             of the source file.
 * @throws IOException if the copying fails.
 *///from w  ww  . j a  v a  2 s .com
public static void copyFile(File sourceFile, File destFile, boolean overwrite, boolean preserveLastModified)
        throws IOException {

    if (overwrite || !destFile.exists() || destFile.lastModified() < sourceFile.lastModified()) {
        org.apache.commons.io.FileUtils.copyFile(sourceFile, destFile, preserveLastModified);
    }
}

From source file:com.alibaba.antx.util.ZipUtil.java

/**
 * /*from   ww  w  .  jav a2  s .  co  m*/
 *
 * @param todir     
 * @param zipStream ?
 * @param zipEntry  zip
 * @param overwrite ?
 * @throws IOException Zip?
 */
protected static void extractFile(File todir, InputStream zipStream, ZipEntry zipEntry, boolean overwrite)
        throws IOException {
    String entryName = zipEntry.getName();
    Date entryDate = new Date(zipEntry.getTime());
    boolean isDirectory = zipEntry.isDirectory();
    File targetFile = FileUtil.getFile(todir, entryName);

    if (!overwrite && targetFile.exists() && targetFile.lastModified() >= entryDate.getTime()) {
        log.debug("Skipping " + targetFile + " as it is up-to-date");
        return;
    }

    log.info("expanding " + entryName + " to " + targetFile);

    if (isDirectory) {
        targetFile.mkdirs();
    } else {
        File dir = targetFile.getParentFile();

        dir.mkdirs();

        byte[] buffer = new byte[8192];
        int length = 0;
        OutputStream ostream = null;

        try {
            ostream = new BufferedOutputStream(new FileOutputStream(targetFile), 8192);

            while ((length = zipStream.read(buffer)) >= 0) {
                ostream.write(buffer, 0, length);
            }
        } finally {
            if (ostream != null) {
                try {
                    ostream.close();
                } catch (IOException e) {
                }
            }
        }
    }

    targetFile.setLastModified(entryDate.getTime());
}

From source file:com.ikanow.aleph2.data_model.utils.ProcessUtils.java

/**
 * Looks in /proc/<pid> and pulls the date of the file
 * //w ww .ja  va 2 s  .c  om
 * @param pid
 * @return
 */
private static long getDateOfPid(final String pid) {
    final File file = new File(PID_PREFIX + pid);
    return file.lastModified();
}

From source file:MakeDirectories.java

private static void fileData(File f) {
    System.out.println("Absolute path: " + f.getAbsolutePath() + "\n Can read: " + f.canRead()
            + "\n Can write: " + f.canWrite() + "\n getName: " + f.getName() + "\n getParent: " + f.getParent()
            + "\n getPath: " + f.getPath() + "\n length: " + f.length() + "\n lastModified: "
            + f.lastModified());
    if (f.isFile())
        System.out.println("It's a file");
    else if (f.isDirectory())
        System.out.println("It's a directory");
}

From source file:com.beetle.framework.util.OtherUtil.java

/**
 * ?//  w ww.ja  va2  s .c o  m
 * 
 * @param filename
 * @return 0-????0
 */
public static long getFileLastModified(String filename) {
    long l = 0;
    File f = new File(filename);
    if (f.exists()) {
        try {
            l = f.lastModified();
        } catch (SecurityException se) {
            l = 0;
            se.printStackTrace();
        }
    }
    return l;
}

From source file:FileUtil.java

/**
 * Gets file date and time./*from   w  ww . ja va 2  s  . c om*/
 *
 * @param url The URL of the file for which date and time will be returned.
 * @return Returns long value which is the date and time of the file. If any error
 *         occures returns -1 (=no file date and time available).
 */
public static long getFileDateTime(URL url) {
    if (url == null) {
        return -1;
    }

    String fileName = url.getFile();
    if (fileName.charAt(0) == '/' || fileName.charAt(0) == '\\') {
        fileName = fileName.substring(1, fileName.length());
    }

    try {
        File file = new File(fileName);
        // File name must be a file or a directory.
        if (!file.isDirectory() && !file.isFile()) {
            return -1;
        }

        return file.lastModified();
    } catch (java.lang.Exception e) { // Trap all Exception based exceptions and return -1.
        return -1;
    }
}

From source file:com.blork.anpod.util.BitmapUtils.java

private static void manageCache(String slug, Context context) {
    String filename = slug + ".jpg";
    File folder;/* w ww . java 2  s  . c  om*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        folder = new File(Environment.getExternalStorageDirectory() + File.separator + "Android"
                + File.separator + "data" + File.separator + "com.blork.anpod" + File.separator + "cache");
    } else {
        folder = context.getCacheDir();
    }

    int cacheSize = 15;
    Log.w("", "current file: " + filename);
    Log.w("", "Managing cache");
    File[] files = folder.listFiles();
    if (files == null || files.length <= cacheSize) {
        Log.w("", "Cache size is fine");
        return;
    }

    int count = files.length;

    Arrays.sort(files, new Comparator<File>() {
        public int compare(File f1, File f2) {
            return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
        }
    });

    for (File f : files) {
        if (count > cacheSize && !filename.equals(f.getName())) {
            Log.w("", "Deleting " + f.getName());
            f.delete();
            count--;
        }
    }
}

From source file:com.parse.ParseKeyValueCache.java

static void saveToKeyValueCache(String key, String value) {
    synchronized (MUTEX_IO) {
        File prior = getKeyValueCacheFile(key);
        if (prior != null) {
            prior.delete();//  w ww.j  a  v a 2  s.  co  m
        }
        File f = createKeyValueCacheFile(key);
        try {
            ParseFileUtils.writeByteArrayToFile(f, value.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // do nothing
        } catch (IOException e) {
            // do nothing
        }

        // Check if we should kick out old cache entries
        File[] files = getKeyValueCacheDir().listFiles();
        // We still need this check since dir.mkdir() may fail
        if (files == null || files.length == 0) {
            return;
        }

        int numFiles = files.length;
        int numBytes = 0;
        for (File file : files) {
            numBytes += file.length();
        }

        // If we do not need to clear the cache, simply return
        if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) {
            return;
        }

        // We need to kick out some cache entries.
        // Sort oldest-first. We touch on read so mtime is really LRU.
        // Sometimes (i.e. tests) the time of lastModified isn't granular enough,
        // so we resort
        // to sorting by the file name which is always prepended with time in ms
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File f1, File f2) {
                int dateCompare = Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
                if (dateCompare != 0) {
                    return dateCompare;
                } else {
                    return f1.getName().compareTo(f2.getName());
                }
            }
        });

        for (File file : files) {
            numFiles--;
            numBytes -= file.length();
            file.delete();

            if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) {
                break;
            }
        }
    }
}

From source file:com.ikanow.aleph2.data_model.utils.ProcessUtils.java

/**
 * Checks if a process is still running with the given pid and started
 * at the given date// w  w  w . j a  v a 2 s  .c o m
 * 
 * @param pid
 * @param date
 * @return
 */
private static boolean isRunning(String pid, Long date) {
    final File pid_file = new File("/proc/" + pid);
    return pid_file.exists() && (pid_file.lastModified() == date);
}

From source file:Main.java

/**
 * To check if the app has opened ever(most time).
 * //from   ww  w  .ja va2  s  .  c om
 * @param  context     context
 * @param  packageName package name
 * @return             true if the app has actived, otherwise return false
 */
public static boolean isAppActivated(Context context, String packageName) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        if (packageInfo == null) {
            return false;
        }

        ApplicationInfo appInfo = packageInfo.applicationInfo;
        File file = new File(appInfo.dataDir);

        /* if the /data/data/<packagename> is not existed, return false */
        if (file == null || !file.exists()) {
            return false;
        }

        long lastUpdateTime = packageInfo.lastUpdateTime;
        long lastModifiedTime = file.lastModified();

        /* if the delta time is greater than 1.5s(most time), then the app has activated */
        if (Math.abs(lastModifiedTime - lastUpdateTime) >= 1500) {
            return true;
        }

        return false;
    } catch (NameNotFoundException e) {
        return false;
    }
}