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.omertron.themoviedbapi.AbstractTests.java

/**
 * Read the object back from a file/*from w ww. j  ava 2 s. co  m*/
 *
 * @param <T>
 * @param filename
 * @return
 */
private static <T> T readObject(final String baseFilename) {
    String filename = baseFilename + FILENAME_EXT;
    File serFile = new File(filename);

    if (serFile.exists()) {
        long diff = System.currentTimeMillis() - serFile.lastModified();
        if (diff < TimeUnit.HOURS.toMillis(1)) {
            LOG.info("File '{}' is current, no need to reacquire", filename);
        } else {
            LOG.info("File '{}' is too old, re-acquiring", filename);
            return null;
        }
    } else {
        LOG.info("File '{}' doesn't exist", filename);
        return null;
    }

    LOG.info("Reading object from '{}'", filename);
    try {
        byte[] serObject = FileUtils.readFileToByteArray(serFile);
        return (T) SerializationUtils.deserialize(serObject);
    } catch (IOException ex) {
        LOG.info("Failed to read {}: {}", filename, ex.getMessage(), ex);
        return null;
    }
}

From source file:org.eclipse.virgo.ide.jdt.internal.core.util.ClasspathUtils.java

/**
 * Adjusts the last modified timestamp on the source root and META-INF directory to reflect the
 * same date as the MANIFEST.MF./*from w  w w  .j  av  a  2  s .  c  o  m*/
 * <p>
 * Note: this is a requirement for the dependency resolution.
 */
public static void adjustLastModifiedDate(IJavaProject javaProject, boolean testBundle) {
    File manifest = BundleManifestUtils.locateManifestFile(javaProject, testBundle);
    if (manifest != null && manifest.canRead()) {
        long lastmodified = manifest.lastModified();
        File metaInfFolder = manifest.getParentFile();
        if (metaInfFolder != null
                && metaInfFolder.getName().equals(BundleManifestCorePlugin.MANIFEST_FOLDER_NAME)
                && metaInfFolder.canWrite()) {
            metaInfFolder.setLastModified(lastmodified);
            File srcFolder = metaInfFolder.getParentFile();
            if (srcFolder != null && srcFolder.canWrite()) {
                srcFolder.setLastModified(lastmodified);
            }
        }
    }
}

From source file:com.demandware.vulnapp.util.Helpers.java

public static File[] lastFileModifiedByExt(File dir, String ext) {
    File[] files = dir.listFiles(new FileFilter() {
        public boolean accept(File file) {
            return file.isFile() && Helpers.getFileExtension(file.getName()).equals(ext);
        }//w  w w.  j  a  v  a2 s  . c o  m
    });

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

    return files;
}

From source file:io.treefarm.plugins.haxe.utils.HaxelibHelper.java

public static int injectPomHaxelib(String artifactId, String artifactVersion, String artifactType,
        File artifactFile, Logger logger) {
    //File unpackDirectory = getHaxelibDirectoryForArtifactAndInitialize(artifactId, artifactVersion, logger);
    File unpackDir = getLibUnpackPath(artifactId, artifactVersion);

    if (!unpackDir.exists() || artifactFile.lastModified() > unpackDir.lastModified()) {
        File libDir = getHaxelibDirectoryForArtifactAndInitialize(artifactId, artifactVersion, logger);

        if (unpackDir.exists()) {
            FileUtils.deleteQuietly(unpackDir);
        }/*  w  w  w.j ava2 s . c  o m*/
        if (libDir.exists()) {
            FileUtils.deleteQuietly(libDir);
        }

        UnpackHelper unpackHelper = new UnpackHelper() {
        };
        DefaultUnpackMethods unpackMethods = new DefaultUnpackMethods(logger);
        try {
            unpackHelper.unpack(unpackDir, artifactType, artifactFile, unpackMethods, null);
        } catch (Exception e) {
            logger.error(String.format("Can't unpack %s: %s", artifactId, e));
        }

        for (String fileName : unpackDir.list()) {
            if (artifactType.equals("jar")) {
                fileName = getUniqueLibPath(artifactId, artifactVersion);
            }
            File firstFile = new File(unpackDir, fileName);
            firstFile.renameTo(libDir);
            break;
        }

        unpackDir.setLastModified(artifactFile.lastModified());
    }

    int returnValue = 0;

    // set to specified version
    returnValue = setVersionFor(artifactId, artifactVersion, logger);

    return returnValue;
}

From source file:net.grinder.util.LogCompressUtil.java

/**
 * Compress multiple Files./*from w  ww .  j  av a  2 s . c  o  m*/
 * 
 * @param logFiles
 *            files to be compressed
 * @return compressed file byte array
 */
public static byte[] compressFile(File[] logFiles) {
    FileInputStream fio = null;
    ByteArrayOutputStream out = null;
    ZipOutputStream zos = null;
    try {

        out = new ByteArrayOutputStream();
        zos = new ZipOutputStream(out);
        for (File each : logFiles) {
            try {
                fio = new FileInputStream(each);
                ZipEntry zipEntry = new ZipEntry(each.getName());
                zipEntry.setTime(each.lastModified());
                zos.putNextEntry(zipEntry);
                byte[] buffer = new byte[COMPRESS_BUFFER_SIZE];
                int count = 0;
                while ((count = fio.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
                    zos.write(buffer, 0, count);
                }
                zos.closeEntry();
            } catch (IOException e) {
                LOGGER.error("Error occurs while compress {}", each.getAbsolutePath());
                LOGGER.error("Details", e);
            } finally {
                IOUtils.closeQuietly(fio);
            }
        }
        zos.finish();
        zos.flush();
        return out.toByteArray();
    } catch (IOException e) {
        LOGGER.info("Error occurs while compress log : {} ", e.getMessage());
        LOGGER.debug("Details", e);
        return null;
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(fio);
        IOUtils.closeQuietly(out);
    }
}

From source file:gov.nih.nci.cacisweb.util.CaCISUtil.java

/**
 * //from w  ww . ja  v  a 2 s.c  o  m
 * @param property
 * @param defaultValue
 * @return
 * @throws PropertyFileLoadException
 */
public static void setProperty(String propertyName, String propertyValue) throws CaCISWebException {

    URL propsUrl = CaCISUtil.class.getClassLoader().getResource(CaCISWebConstants.COM_PROPERTIES_FILE_NAME);
    File propertiesFile = new File(propsUrl.getPath());

    if (properties == null || propertiesFile.lastModified() > lastModified) {
        properties = new Properties();

        try {
            properties.load(propsUrl.openStream());
            properties.setProperty(propertyName, propertyValue);
            FileOutputStream fileOutputStream = new FileOutputStream(new File(propsUrl.getPath()));
            properties.store(fileOutputStream, "Updated Property: " + propertyName);
        } catch (FileNotFoundException ex) {
            throw new CaCISWebException(
                    String.format("The properties file %s does not exist or is not readable|writable",
                            propertiesFile.getAbsolutePath()),
                    ex);
        } catch (IOException ex) {
            throw new CaCISWebException(String.format(
                    "An error was encountered while attempting to read|write the properties file %s",
                    propertiesFile.getAbsolutePath()), ex);
        }
    }
}

From source file:atg.tools.dynunit.test.util.FileUtil.java

public static void forceComponentScope(final File file, final String scope) throws IOException {
    final long oldLastModified = getConfigFileLastModified(file);
    if (oldLastModified < file.lastModified()) {
        final File tempFile = newTempFile();
        BufferedWriter out = null;
        BufferedReader in = null;
        try {//from w  w  w. j av  a2s.c  o m
            out = new BufferedWriter(new FileWriter(tempFile));
            in = new BufferedReader(new FileReader(file));
            for (String line = in.readLine(); line != null; line = in.readLine()) {
                if (line.contains("$scope")) {
                    out.write("$scope=");
                    out.write(scope);
                } else {
                    out.write(line);
                }
                out.newLine();
            }
            FileUtils.copyFile(tempFile, file);
            setConfigFileLastModified(file);
            dirty = true;
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

From source file:com.phonegap.DirectoryManager.java

/**
 * This method will determine the file properties of the file specified
 * by the filePath.  Creates a JSONObject with name, lastModifiedDate and 
 * size properties.//  w w  w  .  j av  a2s  .c o  m
 * 
 * @param filePath the file to get the properties of
 * @return a JSONObject with the files properties
 */
protected static JSONObject getFile(String filePath) {
    File fp = new File(filePath);

    JSONObject obj = new JSONObject();
    try {
        obj.put("name", fp.getAbsolutePath());
        obj.put("lastModifiedDate", new Date(fp.lastModified()).toString());
        obj.put("size", fp.length());
    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }

    return obj;
}

From source file:gov.nih.nci.cacisweb.util.CaCISUtil.java

/**
 * //from   w ww . j a v  a 2s  . co m
 * @param property
 * @param defaultValue
 * @return
 * @throws PropertyFileLoadException
 */
public static String getProperty(String property, String defaultValue) throws CaCISWebException {

    URL propsUrl = CaCISUtil.class.getClassLoader().getResource(CaCISWebConstants.COM_PROPERTIES_FILE_NAME);
    File propertiesFile = new File(propsUrl.getPath());

    if (properties == null || propertiesFile.lastModified() > lastModified) {
        properties = new Properties();

        try {
            properties.load(propsUrl.openStream());
        } catch (FileNotFoundException ex) {
            throw new CaCISWebException(
                    String.format("The properties file %s does not exist or is not readable",
                            propertiesFile.getAbsolutePath()),
                    ex);
        } catch (IOException ex) {
            throw new CaCISWebException(
                    String.format("An error was encountered while attempting to read the properties file %s",
                            propertiesFile.getAbsolutePath()),
                    ex);
        }
    }

    log.debug(property + " = " + properties.getProperty(property, defaultValue));

    return properties.getProperty(property, defaultValue);
}

From source file:setiquest.renderer.Utils.java

/**
 * Remove old BSON files.// www .j  a  va2 s  .  c  o m
 * @param bsonDir the full path directory containing the son data files.
 * @param maxDays the maximum days a bson file should live.
 */
public static int purgeBsonDir(String bsonDir, long maxDays) {
    File directory = new File(bsonDir);
    int count = 0;

    if (directory.exists()) {
        File[] listFiles = directory.listFiles();
        long purgeTime = System.currentTimeMillis() - (maxDays * 24 * 60 * 60 * 1000);

        for (File listFile : listFiles) {
            if (listFile.lastModified() < purgeTime) {
                if (!listFile.delete()) {
                    Log.log("Unable to delete file: " + listFile);
                } else {
                    count++;
                }
            }
        }
    } else {
        Log.log("Files were not deleted, directory " + bsonDir + " does'nt exist!");
        System.out.println("Files were not deleted, directory " + bsonDir + " does'nt exist!");
    }

    return count;
}