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.ibm.jaggr.core.util.ZipUtil.java

/**
 * Extracts the directory entry to the location specified by {@code dir}
 *
 * @param entry/*  w  ww  .  j av  a  2s. co m*/
 *            the {@link ZipEntry} object for the directory being extracted
 * @param dir
 *            the {@link File} object for the target directory
 *
 * @throws IOException
 */
private static void extractDirectory(ZipEntry entry, File dir) 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, dir });
    }

    dir.mkdir(); // May fail if the directory has already been created
    if (!dir.setLastModified(entry.getTime())) {
        throw new IOException("Failed to set last modified time for " + dir.getAbsolutePath()); //$NON-NLS-1$
    }

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

From source file:com.wavemaker.commons.util.IOUtils.java

/**
 * Touch a file (see touch(1)). If the file doesn't exist, create it as an empty file. If it does exist, update its
 * modification time./*from w  ww.jav  a2s  .  c  o m*/
 *
 * @param f The File.
 * @throws IOException
 */
public static void touch(File f) throws IOException {

    if (!f.exists()) {
        FileWriter fw = new FileWriter(f);
        fw.close();
    } else {
        f.setLastModified(System.currentTimeMillis());
    }
}

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

public static void setLastModified(String fileName, long modifiedTime) throws Exception {

    File file = new File(fileName);
    file.setLastModified(modifiedTime);
}

From source file:com.mgmtp.perfload.perfalyzer.util.IoUtilities.java

private static void extractEntry(final ZipFile zf, final ZipEntry entry, final File destDir)
        throws IOException {
    File file = new File(destDir, entry.getName());

    if (entry.isDirectory()) {
        file.mkdirs();//ww w. j a  v a 2  s .  c  o  m
    } else {
        new File(file.getParent()).mkdirs();

        try (InputStream is = zf.getInputStream(entry); FileOutputStream os = new FileOutputStream(file)) {
            copy(Channels.newChannel(is), os.getChannel());
        }
        // preserve modification time; must be set after the stream is closed
        file.setLastModified(entry.getTime());
    }
}

From source file:com.ms.commons.test.common.FileUtil.java

private static void doCopyDirectory(File srcDir, File destDir, boolean preserveFileDate, FileFilter filter)
        throws IOException {
    if (destDir.exists()) {
        if (destDir.isDirectory() == false) {
            throw new IOException("Destination '" + destDir + "' exists but is not a directory");
        }/*from   w  w w  .  j  a v  a 2 s. co m*/
    } else {
        if (destDir.mkdirs() == false) {
            throw new IOException("Destination '" + destDir + "' directory cannot be created");
        }
        if (preserveFileDate) {
            destDir.setLastModified(srcDir.lastModified());
        }
    }
    if (destDir.canWrite() == false) {
        throw new IOException("Destination '" + destDir + "' cannot be written to");
    }
    // recurse
    File[] files = srcDir.listFiles();
    if (files == null) { // null if security restricted
        throw new IOException("Failed to list contents of " + srcDir);
    }
    for (int i = 0; i < files.length; i++) {
        if (filter.accept(files[i])) {
            File copiedFile = new File(destDir, files[i].getName());
            if (files[i].isDirectory()) {
                doCopyDirectory(files[i], copiedFile, preserveFileDate, filter);
            } else {
                doCopyFile(files[i], copiedFile, preserveFileDate);
            }
        }
    }
}

From source file:gov.nasa.ensemble.common.io.FileUtilities.java

/**
 * recursive method to set modified time to a file/directory
 * /*from   w w w.  jav  a2  s  .  c om*/
 * @param file
 * @param time
 */
public static void setLastModified(File file, long time) {
    if (file.isDirectory()) {
        for (File currentFile : file.listFiles()) {
            setLastModified(currentFile, time);
        }
    } else {
        file.setLastModified(time);
    }
}

From source file:org.syncany.tests.unit.util.TestFileUtil.java

public static File copyFile(File fromFile, File toFile) throws IOException {
    InputStream in = new FileInputStream(fromFile);
    OutputStream out = new FileOutputStream(toFile);

    byte[] buffer = new byte[1024];

    int length;//from w w w. j  a v  a  2  s.  c om
    // copy the file content in bytes
    while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);
    }

    in.close();
    out.close();

    toFile.setLastModified(fromFile.lastModified()); // Windows changes last modified when copying file

    return toFile;
}

From source file:com.meltmedia.cadmium.core.FileSystemManager.java

public static void copyAllContent(final String source, final String target, final boolean ignoreHidden)
        throws Exception {
    File sourceFile = new File(source);
    File targetFile = new File(target);
    if (!targetFile.exists()) {
        targetFile.mkdirs();// w  w  w.ja va 2s .co  m
    }
    if (sourceFile.exists() && sourceFile.canRead() && sourceFile.isDirectory() && targetFile.exists()
            && targetFile.canWrite() && targetFile.isDirectory()) {
        List<File> copyList = new ArrayList<File>();
        FilenameFilter filter = new FilenameFilter() {

            @Override
            public boolean accept(File file, String name) {
                return !ignoreHidden || !name.startsWith(".");
            }

        };
        File files[] = sourceFile.listFiles(filter);
        if (files.length > 0) {
            copyList.addAll(Arrays.asList(files));
        }
        for (int index = 0; index < copyList.size(); index++) {
            File aFile = copyList.get(index);
            String relativePath = aFile.getAbsoluteFile().getAbsolutePath()
                    .replaceFirst(sourceFile.getAbsoluteFile().getAbsolutePath(), "");
            if (aFile.isDirectory()) {
                File newDir = new File(target, relativePath);
                if (newDir.mkdir()) {
                    newDir.setExecutable(aFile.canExecute(), false);
                    newDir.setReadable(aFile.canRead(), false);
                    newDir.setWritable(aFile.canWrite(), false);
                    newDir.setLastModified(aFile.lastModified());
                    files = aFile.listFiles(filter);
                    if (files.length > 0) {
                        copyList.addAll(index + 1, Arrays.asList(files));
                    }
                } else {
                    log.warn("Failed to create new subdirectory \"{}\" in the target path \"{}\".",
                            relativePath, target);
                }
            } else {
                File newFile = new File(target, relativePath);
                if (newFile.createNewFile()) {
                    FileInputStream inStream = null;
                    FileOutputStream outStream = null;
                    try {
                        inStream = new FileInputStream(aFile);
                        outStream = new FileOutputStream(newFile);
                        streamCopy(inStream, outStream);
                    } finally {
                        if (inStream != null) {
                            try {
                                inStream.close();
                            } catch (Exception e) {
                            }
                        }
                        if (outStream != null) {
                            try {
                                outStream.flush();
                            } catch (Exception e) {
                            }
                            try {
                                outStream.close();
                            } catch (Exception e) {
                            }
                        }
                    }
                    newFile.setExecutable(aFile.canExecute(), false);
                    newFile.setReadable(aFile.canRead(), false);
                    newFile.setWritable(aFile.canWrite(), false);
                    newFile.setLastModified(aFile.lastModified());
                }
            }
        }
    }
}

From source file:snake.server.PathUtils.java

public static void receiveFile(Path localDirectory, ArrayList<PathDescriptor> localDescriptors,
        PathDescriptor inDescriptor, RemoteInputStream ristream) throws IOException {
    InputStream istream = RemoteInputStreamClient.wrap(ristream);
    FileOutputStream ostream = null;
    Path filePath = localDirectory.resolve(inDescriptor.relative_path).toAbsolutePath();
    File file = filePath.toFile();
    try {//from   ww w. j  a  v  a2 s  . co m
        file.getParentFile().mkdirs();
        file.createNewFile();
        ostream = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = istream.read(buffer)) >= 0) {
            ostream.write(buffer, 0, bytesRead);
        }
        ostream.flush();
    } finally {
        try {
            if (istream != null) {
                istream.close();
            }
        } finally {
            if (ostream != null) {
                ostream.close();
            }
        }
        file.setLastModified(inDescriptor.lastModified.getTime());
        PathDescriptor localPD = PathDescriptor.FindIfEqualRelativePath(localDescriptors, inDescriptor);
        if (localPD != null) {
            localPD.status = PathDescriptor.Status.Modified;
            localPD.statusTimePoint = new Date();
            localPD.lastModified = inDescriptor.lastModified;
            localPD.lastCreationTime = inDescriptor.lastCreationTime;
        } else {
            localPD = new PathDescriptor();
            localPD.relative_path = inDescriptor.relative_path;
            localPD.status = PathDescriptor.Status.Modified;
            localPD.statusTimePoint = new Date();
            localPD.lastModified = inDescriptor.lastModified;
            localPD.lastCreationTime = inDescriptor.lastCreationTime;
            localDescriptors.add(localPD);
        }
    }
}

From source file:org.sakaiproject.portal.charon.test.PortalTestFileUtils.java

/**
 * unpack a segment from a zip// w  w w.j ava 2  s.  c  om
 * 
 * @param addsi
 * @param packetStream
 * @param version
 */
public static void unpack(InputStream source, File destination) throws IOException {
    ZipInputStream zin = new ZipInputStream(source);
    ZipEntry zipEntry = null;
    FileOutputStream fout = null;
    try {
        byte[] buffer = new byte[4096];
        while ((zipEntry = zin.getNextEntry()) != null) {

            long ts = zipEntry.getTime();
            // the zip entry needs to be a full path from the
            // searchIndexDirectory... hence this is correct

            File f = new File(destination, zipEntry.getName());
            if (log.isDebugEnabled())
                log.debug("         Unpack " + f.getAbsolutePath());
            f.getParentFile().mkdirs();

            fout = new FileOutputStream(f);
            int len;
            while ((len = zin.read(buffer)) > 0) {
                fout.write(buffer, 0, len);
            }
            zin.closeEntry();
            fout.close();
            f.setLastModified(ts);
        }
    } finally {
        try {
            fout.close();
        } catch (Exception ex) {
        }
    }
}