Example usage for java.io File setLastModified

List of usage examples for java.io File setLastModified

Introduction

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

Prototype

public boolean setLastModified(long time) 

Source Link

Document

Sets the last-modified time of the file or directory named by this abstract pathname.

Usage

From source file:ubic.basecode.util.FileTools.java

/**
 * Create or update the modification date of the given file. If the file does not exist, create it.
 * /* w w w  . j  a v  a2s  .  c o m*/
 * @param f
 * @throws IOException
 */
public static void touch(File f) throws IOException {
    if (!f.exists()) {
        FileWriter w = new FileWriter(f);
        w.append("");
        w.close();
    }
    f.setLastModified(new Date().getTime());
}

From source file:org.xmlactions.common.io.ResourceUtils.java

/**
 * mkdir for folderName and set the lastModifiedDate to lastModifiedDate
 * /*  w  ww . j  a va  2  s. co m*/
 * @return -1 if unable to mkdir, -2 if unable to setLastModified, 0 if ok
 * 
 */
public static int mkdir(String folderName, long lastModifiedDate) {

    File file = new File(folderName);
    int result = mkdir(file);
    if (result != 0) {
        return (result);
    }
    if (file.setLastModified(lastModifiedDate) == false) {
        return (-2);
    }
    return (0);
}

From source file:org.eclipse.wst.json.core.internal.download.HttpClientProvider.java

public static File getFile(URL url) throws IOException {
    if (url == null) {
        return null;
    }//w w  w  . j av  a 2 s. c om
    if (PROTOCOL_FILE.equals(url.getProtocol()) || PROTOCOL_PLATFORM.equalsIgnoreCase(url.getProtocol())) {
        File file;
        try {
            file = new File(new URI(url.toExternalForm()));
        } catch (Exception e) {
            file = new File(url.getFile());
        }
        if (!file.exists()) {
            return null;
        }
        return file;
    }
    File file = getCachedFile(url);
    long urlLastModified = getLastModified(url);
    if (file.exists()) {
        long lastModified = file.lastModified();
        if (urlLastModified > lastModified) {
            file = download(file, url);
            if (file != null) {
                file.setLastModified(urlLastModified);
            }
        }
    } else {
        file = download(file, url);
        if (file != null && urlLastModified > -1) {
            file.setLastModified(urlLastModified);
        }
    }
    return file;
}

From source file:org.apache.wookie.w3c.util.WidgetPackageUtils.java

/**
 * uses apache commons compress to unpack all the zip entries into a target folder
 * partly adapted from the examples on the apache commons compress site, and an
 * example of generic Zip unpacking. Note this iterates over the ZipArchiveEntry enumeration rather
 * than use the more typical ZipInputStream parsing model, as according to the doco it will
 * more reliably read the entries correctly. More info here: http://commons.apache.org/compress/zip.html
 * @param zipfile the Zip File to unpack
 * @param targetFolder the folder into which to unpack the Zip file
 * @throws IOException/*from ww w .  j a v a 2s  . c om*/
 */
@SuppressWarnings("unchecked")
public static void unpackZip(ZipFile zipfile, File targetFolder) throws IOException {
    targetFolder.mkdirs();
    BufferedOutputStream out = null;
    InputStream in = null;
    ZipArchiveEntry zipEntry;

    Enumeration entries = zipfile.getEntries();
    try {
        while (entries.hasMoreElements()) {
            zipEntry = (ZipArchiveEntry) entries.nextElement();
            // Don't add directories - use mkdirs instead
            if (!zipEntry.isDirectory()) {
                File outFile = new File(targetFolder, zipEntry.getName());

                // Ensure that the parent Folder exists
                if (!outFile.getParentFile().exists()) {
                    outFile.getParentFile().mkdirs();
                }
                // Read the entry
                in = new BufferedInputStream(zipfile.getInputStream(zipEntry));
                out = new BufferedOutputStream(new FileOutputStream(outFile));
                IOUtils.copy(in, out);
                // Restore time stamp
                outFile.setLastModified(zipEntry.getTime());

                // Close File
                out.close();
                // Close Stream
                in.close();
            }
        }

    }
    // We'll catch this exception to close the file otherwise it remains locked
    catch (IOException ex) {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.flush();
            out.close();
        }
        // And throw it again
        throw ex;
    }
}

From source file:com.ibm.jaggr.core.util.ZipUtil.java

/**
 * Extracts the file entry to the location specified by {@code file}
 *
 * @param entry//  w  ww .  j a  va2s.c  om
 *            the {@link ZipEntry} object for the directory being extracted
 * @param zipIn
 *            the zip input stream to read from
 * @param file
 *            the {@link File} object for the target file
 * @throws IOException
 */
private static void extractFile(ZipEntry entry, ZipInputStream zipIn, File file) throws IOException {
    final String sourceMethod = "extractFile"; //$NON-NLS-1$
    final boolean isTraceLogging = log.isLoggable(Level.FINER);
    if (isTraceLogging) {
        log.entering(sourceClass, sourceMethod, new Object[] { entry, zipIn, file });
    }

    Files.createParentDirs(file);
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
    try {
        IOUtils.copy(zipIn, bos);
    } finally {
        bos.close();
    }
    if (!file.setLastModified(entry.getTime())) {
        throw new IOException("Failed to set last modified time for " + file.getAbsolutePath()); //$NON-NLS-1$
    }

    if (isTraceLogging) {
        log.exiting(sourceClass, sourceMethod);
    }
}

From source file:org.apache.hadoop.gateway.GatewayServer.java

private static void redeployTopology(Topology topology) {
    File topologyFile = new File(topology.getUri());
    long start = System.currentTimeMillis();
    long limit = 1000L; // One second.
    long elapsed = 1;
    while (elapsed <= limit) {
        try {// w  w  w .j  a va  2s .com
            long origTimestamp = topologyFile.lastModified();
            long setTimestamp = Math.max(System.currentTimeMillis(), topologyFile.lastModified() + elapsed);
            if (topologyFile.setLastModified(setTimestamp)) {
                long newTimstamp = topologyFile.lastModified();
                if (newTimstamp > origTimestamp) {
                    break;
                } else {
                    Thread.sleep(10);
                    elapsed = System.currentTimeMillis() - start;
                    continue;
                }
            } else {
                log.failedToRedeployTopology(topology.getName());
                break;
            }
        } catch (InterruptedException e) {
            log.failedToRedeployTopology(topology.getName(), e);
            e.printStackTrace();
        }
    }
}

From source file:net.lightbody.bmp.proxy.jetty.util.JarResource.java

public static void extract(Resource resource, File directory, boolean deleteOnExit) throws IOException {
    if (log.isDebugEnabled())
        log.debug("Extract " + resource + " to " + directory);
    JarInputStream jin = new JarInputStream(resource.getInputStream());
    JarEntry entry = null;//from   w  w  w. ja  va 2s  .c o  m
    while ((entry = jin.getNextJarEntry()) != null) {
        File file = new File(directory, entry.getName());
        if (entry.isDirectory()) {
            // Make directory
            if (!file.exists())
                file.mkdirs();
        } else {
            // make directory (some jars don't list dirs)
            File dir = new File(file.getParent());
            if (!dir.exists())
                dir.mkdirs();

            // Make file
            FileOutputStream fout = null;
            try {
                fout = new FileOutputStream(file);
                IO.copy(jin, fout);
            } finally {
                IO.close(fout);
            }

            // touch the file.
            if (entry.getTime() >= 0)
                file.setLastModified(entry.getTime());
        }
        if (deleteOnExit)
            file.deleteOnExit();
    }
}

From source file:org.robovm.gradle.tasks.AbstractRoboVMTask.java

private static void extractTarGz(File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;/*from  w  w w. j  a  va2s.c o m*/
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
            f.setLastModified(entry.getLastModifiedDate().getTime());
            if (entry instanceof TarArchiveEntry) {
                int mode = ((TarArchiveEntry) entry).getMode();
                if ((mode & 00100) > 0) {
                    // Preserve execute permissions
                    f.setExecutable(true, (mode & 00001) == 0);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.commoncrawl.util.HDFSBlockTransferUtility.java

private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
    if (destFile.exists() && destFile.isDirectory()) {
        throw new IOException("Destination '" + destFile + "' exists but is a directory");
    }//  w  w w .ja  v  a  2  s  .  c  o m

    FileInputStream input = new FileInputStream(srcFile);
    try {
        FileOutputStream output = new FileOutputStream(destFile);
        try {
            copyLarge(input, output);
        } finally {
            IOUtils.closeQuietly(output);
        }
    } finally {
        IOUtils.closeQuietly(input);
    }

    if (srcFile.length() != destFile.length()) {
        throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
    }
    if (preserveFileDate) {
        destFile.setLastModified(srcFile.lastModified());
    }
}

From source file:com.piaoyou.util.FileUtil.java

public static boolean touch(String fileName) {
    try {//from w w  w  . j  a v  a  2  s.co m
        File file = new File(fileName);
        if (file.exists() == false) {
            file.createNewFile();
        } else {
            file.setLastModified(System.currentTimeMillis());
        }
    } catch (Throwable t) {
        return false;
    }
    return true;
}