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.adaptris.core.fs.OlderThanTest.java

@Test
public void testOlderThan() throws Exception {
    OlderThan filter = new OlderThan("-PT30S");
    File file = writeFile(TempFileUtils.createTrackedFile(filter));
    file.setLastModified(yesterday());
    assertTrue(filter.accept(file));/*  w  ww .j  a va 2s .com*/
}

From source file:com.adaptris.core.fs.OlderThanTest.java

@Test
public void testOlderThanFutureSpec() throws Exception {
    OlderThan filter = new OlderThan("PT1H");
    File file = writeFile(TempFileUtils.createTrackedFile(filter));
    file.setLastModified(yesterday());
    assertTrue(filter.accept(file));/*  w ww  .  ja  v  a 2  s  . c  o m*/
}

From source file:com.adaptris.core.fs.OlderThanTest.java

@Test
public void testBadDuration() throws Exception {
    OlderThan filter = new OlderThan("-PXXX");
    File file = writeFile(TempFileUtils.createTrackedFile(filter));
    file.setLastModified(yesterday());
    assertFalse(filter.accept(file));// w  ww . j  a  v  a  2s.c o m
}

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

/**
 * copy a file./*  www  . ja va  2  s  . c  o  m*/
 * 
 * @param sourceFile
 *            is the file we want to copy.
 * @param destFile
 *            is the destination file to copy to.
 */
public static void copyFile(File sourceFile, File destFile) throws Exception {

    try {
        FileInputStream fis = new FileInputStream(sourceFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        copyFileStreams(fis, fos);
    } catch (Exception ex) {
        throw new Exception("Unable to copy file '" + sourceFile.getAbsolutePath() + "' to '"
                + destFile.getAbsolutePath() + "'", ex);
    }
    if (destFile.setLastModified(sourceFile.lastModified()) == false) {
        throw new Exception("Unable to change lastModified time on file:" + destFile.getAbsolutePath()
                + "\nfrom:" + DateTime.getDate(destFile.lastModified(), "dd-MO-yy hh:mm:ss") + " to:"
                + DateTime.getDate(sourceFile.lastModified(), "dd-MO-yy hh:mm:ss"));
    }
}

From source file:com.zimbra.cs.service.util.ItemDataFile.java

public static void extract(InputStream is, boolean meta, Set<MailItem.Type> types, String cset, String dir)
        throws IOException {
    byte[] buf = new byte[TarBuffer.DEFAULT_BLKSIZE];
    TarEntry te;//from   w  ww. j a va 2s. c om
    TarInputStream tis = new TarInputStream(new GZIPInputStream(is), cset == null ? "UTF-8" : cset);

    if (dir == null)
        dir = ".";
    try {
        while ((te = tis.getNextEntry()) != null) {
            if (skip(types, MailItem.Type.of((byte) te.getMajorDeviceId()))) {
                continue;
            }

            File f = new File(dir + File.separator + te.getName());
            FileOutputStream out;

            if (!f.getParent().equals("."))
                f.getParentFile().mkdir();
            if (te.getName().endsWith(".meta")) {
                if (!meta)
                    continue;
                System.out.println(f);
                out = new FileOutputStream(f);
                ItemData id = new ItemData(getData(tis, te));
                out.write(id.encode(2).getBytes("UTF-8"));
            } else {
                int in;

                System.out.println(f);
                out = new FileOutputStream(f);
                while ((in = tis.read(buf)) != -1)
                    out.write(buf, 0, in);
            }
            out.close();
            f.setLastModified(te.getModTime().getTime());
        }
    } finally {
        tis.close();
    }
}

From source file:org.pwsafe.lib.file.PwsFileFactoryTest.java

public void testConcurrentMod() throws Exception {
    final PwsFile pwsFile = PwsFileFactory.loadFile(testV2Filename, new StringBuilder(PASSPHRASE));

    final File file = new File(testV2Filename);
    file.setLastModified(System.currentTimeMillis() + 1000);
    pwsFile.setModified();/*w  w w.j  a v a2  s.c o  m*/
    try {
        pwsFile.save();
        fail("save concurrently modified file without exception");
    } catch (final ConcurrentModificationException e) {
        // ok
    }
    // and after save:
    file.setLastModified(System.currentTimeMillis() + 2000);
    pwsFile.setModified();
    try {
        pwsFile.save();
        fail("save concurrently modified file without exception");
    } catch (final ConcurrentModificationException e) {
        // ok
    }
}

From source file:JarUtils.java

public static void unjar(InputStream in, File dest) throws IOException {
    if (!dest.exists()) {
        dest.mkdirs();/*w  ww  . j a v  a 2  s  .c o  m*/
    }
    if (!dest.isDirectory()) {
        throw new IOException("Destination must be a directory.");
    }
    JarInputStream jin = new JarInputStream(in);
    byte[] buffer = new byte[1024];

    ZipEntry entry = jin.getNextEntry();
    while (entry != null) {
        String fileName = entry.getName();
        if (fileName.charAt(fileName.length() - 1) == '/') {
            fileName = fileName.substring(0, fileName.length() - 1);
        }
        if (fileName.charAt(0) == '/') {
            fileName = fileName.substring(1);
        }
        if (File.separatorChar != '/') {
            fileName = fileName.replace('/', File.separatorChar);
        }
        File file = new File(dest, fileName);
        if (entry.isDirectory()) {
            // make sure the directory exists
            file.mkdirs();
            jin.closeEntry();
        } else {
            // make sure the directory exists
            File parent = file.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }

            // dump the file
            OutputStream out = new FileOutputStream(file);
            int len = 0;
            while ((len = jin.read(buffer, 0, buffer.length)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
            out.close();
            jin.closeEntry();
            file.setLastModified(entry.getTime());
        }
        entry = jin.getNextEntry();
    }
    /* Explicity write out the META-INF/MANIFEST.MF so that any headers such
    as the Class-Path are see for the unpackaged jar
    */
    Manifest mf = jin.getManifest();
    if (mf != null) {
        File file = new File(dest, "META-INF/MANIFEST.MF");
        File parent = file.getParentFile();
        if (parent.exists() == false) {
            parent.mkdirs();
        }
        OutputStream out = new FileOutputStream(file);
        mf.write(out);
        out.flush();
        out.close();
    }
    jin.close();
}

From source file:com.aoyetech.fee.commons.utils.FileUtils.java

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

    final FileInputStream input = new FileInputStream(srcFile);
    try {
        final FileOutputStream output = new FileOutputStream(destFile);
        try {
            IOUtils.copy(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:org.sonatype.nexus.tasklog.TaskLogCleanupTest.java

private File createFile(final String name, final int ageInDays) throws IOException {
    File file = new File(tempTaskFolder, name);
    file.createNewFile();//from   w w w. j a  v a  2s.com
    file.setLastModified(ZonedDateTime.now().minusDays(ageInDays).toInstant().toEpochMilli());
    return file;
}

From source file:com.zotoh.core.util.FileUte.java

private static void copyOneFile(File srcFile, File destFile) throws IOException {
    if (destFile.exists()) {

        if (!destFile.isFile()) {
            throw new IOException("\"" + destFile + "\" exists but is not a valid file");
        }//from   ww w . ja v  a 2s .  c om

        if (!destFile.canWrite()) {
            throw new IOException("Cannot overwrite \"" + destFile + "\"");
        }

    }

    InputStream src = new FileInputStream(srcFile);
    OutputStream out = null;

    try {
        streamToStream(src, out = new FileOutputStream(destFile));
    } finally {
        StreamUte.close(out);
        StreamUte.close(src);
    }

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

    // preserve the file datetime
    destFile.setLastModified(srcFile.lastModified());
}