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:com.clark.func.Functions.java

/**
 * Implements the same behaviour as the "touch" utility on Unix. It creates
 * a new file with size 0 or, if the file exists already, it is opened and
 * closed without modifying it, but updating the file date and time.
 * <p>// w  w w.  j a  v  a2s. c o m
 * NOTE: As from v1.3, this method throws an IOException if the last
 * modified date of the file cannot be set. Also, as from v1.3 this method
 * creates parent directories if they do not exist.
 * 
 * @param file
 *            the File to touch
 * @throws IOException
 *             If an I/O problem occurs
 */
public static void touch(File file) throws IOException {
    if (!file.exists()) {
        OutputStream out = openOutputStream(file);
        closeQuietly(out);
    }
    boolean success = file.setLastModified(System.currentTimeMillis());
    if (!success) {
        throw new IOException("Unable to set the last modification time for " + file);
    }
}

From source file:com.clark.func.Functions.java

/**
 * Internal copy file method.//from   w  w  w.  ja v a 2s  .co  m
 * 
 * @param srcFile
 *            the validated source file, must not be <code>null</code>
 * @param destFile
 *            the validated destination file, must not be <code>null</code>
 * @param preserveFileDate
 *            whether to preserve the file date
 * @throws IOException
 *             if an error occurs
 */
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");
    }

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel input = null;
    FileChannel output = null;
    try {
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);
        input = fis.getChannel();
        output = fos.getChannel();
        long size = input.size();
        long pos = 0;
        long count = 0;
        while (pos < size) {
            count = (size - pos) > FIFTY_MB ? FIFTY_MB : (size - pos);
            pos += output.transferFrom(input, pos, count);
        }
    } finally {
        closeQuietly(output);
        closeQuietly(fos);
        closeQuietly(input);
        closeQuietly(fis);
    }

    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.clark.func.Functions.java

/**
 * Internal copy directory method.//from w w  w .  j av a2s . c o m
 * 
 * @param srcDir
 *            the validated source directory, must not be <code>null</code>
 * @param destDir
 *            the validated destination directory, must not be
 *            <code>null</code>
 * @param filter
 *            the filter to apply, null means copy all directories and files
 * @param preserveFileDate
 *            whether to preserve the file date
 * @param exclusionList
 *            List of files and directories to exclude from the copy, may be
 *            null
 * @throws IOException
 *             if an error occurs
 * @since Commons IO 1.1
 */
private static void doCopyDirectory(File srcDir, File destDir, FileFilter filter, boolean preserveFileDate,
        List<String> exclusionList) throws IOException {
    // recurse
    File[] files = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
    if (files == null) { // null if security restricted
        throw new IOException("Failed to list contents of " + srcDir);
    }
    if (destDir.exists()) {
        if (destDir.isDirectory() == false) {
            throw new IOException("Destination '" + destDir + "' exists but is not a directory");
        }
    } else {
        if (destDir.mkdirs() == false) {
            throw new IOException("Destination '" + destDir + "' directory cannot be created");
        }
    }
    if (destDir.canWrite() == false) {
        throw new IOException("Destination '" + destDir + "' cannot be written to");
    }
    for (File file : files) {
        File copiedFile = new File(destDir, file.getName());
        if (exclusionList == null || !exclusionList.contains(file.getCanonicalPath())) {
            if (file.isDirectory()) {
                doCopyDirectory(file, copiedFile, filter, preserveFileDate, exclusionList);
            } else {
                doCopyFile(file, copiedFile, preserveFileDate);
            }
        }
    }

    // Do this last, as the above has probably affected directory metadata
    if (preserveFileDate) {
        destDir.setLastModified(srcDir.lastModified());
    }
}