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:de.undercouch.gradle.tasks.download.OnlyIfNewerTest.java

/**
 * Tests if the plugin doesn't download a file if the timestamp equals
 * the last-modified header/*from  w  w  w. j  a  v a 2  s .c om*/
 * @throws Exception if anything goes wrong
 */
@Test
public void dontDownloadIfEqual() throws Exception {
    String lm = "Tue, 15 Nov 1994 12:45:26 GMT";
    long expectedlmlong = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).parse(lm)
            .getTime();
    lastModified = lm;

    Download t = makeProjectAndTask();
    t.src(makeSrc(LAST_MODIFIED));
    File dst = folder.newFile();
    FileUtils.writeStringToFile(dst, "Hello");
    dst.setLastModified(expectedlmlong);
    t.dest(dst);
    t.onlyIfNewer(true);
    t.execute();

    long lmlong = dst.lastModified();
    assertEquals(expectedlmlong, lmlong);
    String dstContents = FileUtils.readFileToString(dst);
    assertEquals("Hello", dstContents);
}

From source file:de.undercouch.gradle.tasks.download.OnlyIfNewerTest.java

/**
 * Tests if the plugin doesn't download a file if the timestamp is newer
 * than the last-modified header/*from w  ww  .ja v  a  2  s .co  m*/
 * @throws Exception if anything goes wrong
 */
@Test
public void dontDownloadIfOlder() throws Exception {
    String lm = "Tue, 15 Nov 1994 12:45:26 GMT";
    long expectedlmlong = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).parse(lm)
            .getTime();
    lastModified = lm;

    Download t = makeProjectAndTask();
    t.src(makeSrc(LAST_MODIFIED));
    File dst = folder.newFile();
    FileUtils.writeStringToFile(dst, "Hello");
    dst.setLastModified(expectedlmlong + 1000);
    t.dest(dst);
    t.onlyIfNewer(true);
    t.execute();

    long lmlong = dst.lastModified();
    assertEquals(expectedlmlong + 1000, lmlong);
    String dstContents = FileUtils.readFileToString(dst);
    assertEquals("Hello", dstContents);
}

From source file:de.undercouch.gradle.tasks.download.OnlyIfNewerTest.java

/**
 * Tests if the plugin downloads a file if the timestamp is older than
 * the last-modified header/*from  w w w.j  a  va 2s  .  c  o m*/
 * @throws Exception if anything goes wrong
 */
@Test
public void newerDownload() throws Exception {
    String lm = "Tue, 15 Nov 1994 12:45:26 GMT";
    long expectedlmlong = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).parse(lm)
            .getTime();
    lastModified = lm;

    Download t = makeProjectAndTask();
    t.src(makeSrc(LAST_MODIFIED));
    File dst = folder.newFile();
    FileUtils.writeStringToFile(dst, "Hello");
    dst.setLastModified(expectedlmlong - 1000);
    t.dest(dst);
    t.onlyIfNewer(true);
    t.execute();

    long lmlong = dst.lastModified();
    assertEquals(expectedlmlong, lmlong);
    String dstContents = FileUtils.readFileToString(dst);
    assertEquals("lm: " + lm, dstContents);
}

From source file:com.cloudera.gertrude.file.FileExperimentSpaceLoader.java

private FileAlterationMonitor getMonitor(long pollIntervalMillis) {
    FileAlterationObserver observer = new FileAlterationObserver(dataFile.getParentFile(), new FileFilter() {
        @Override/*w w  w. j  a  v a  2 s  . c  o  m*/
        public boolean accept(File file) {
            return dataFile.equals(file);
        }
    });

    observer.addListener(new FileAlterationListenerAdaptor() {
        @Override
        public void onFileChange(File file) {
            file.setLastModified(System.currentTimeMillis());
            reload(false);
        }
    });
    FileAlterationMonitor m = new FileAlterationMonitor(pollIntervalMillis);
    m.addObserver(observer);
    return m;
}

From source file:com.thoughtworks.go.util.ZipUtilTest.java

@Test
void shouldPreserveFileTimestampWhileGeneratingTheZipFile() throws Exception {
    File file = temporaryFolder.newFile("foo.txt");
    file.setLastModified(1297989100000L); // Set this to any date in the past which is greater than the epoch
    File zip = zipUtil.zip(file, temporaryFolder.newFile("foo.zip"), Deflater.DEFAULT_COMPRESSION);

    ZipFile actualZip = new ZipFile(zip.getAbsolutePath());
    ZipEntry entry = actualZip.getEntry(file.getName());

    assertThat(entry.getTime()).isEqualTo(file.lastModified());
}

From source file:com.izforge.izpack.installer.unpacker.FileUnpacker.java

/**
 * Sets the last-modified timestamp of a file from the pack-file meta-data.
 *
 * @param file the pack file meta-data/*from  w w w. jav  a2  s . co  m*/
 */
protected void setLastModified(PackFile file) {
    // Set file modification time if specified
    if (file.lastModified() >= 0) {
        File f = (tmpTarget != null) ? tmpTarget : target;
        if (!f.setLastModified(file.lastModified())) {
            logger.warning("Failed to set last modified timestamp for: " + target);
        }
    }
}

From source file:com.dmbstream.android.service.DownloadFile.java

private void updateModificationDate(File file) {
    if (file.exists()) {
        boolean ok = file.setLastModified(System.currentTimeMillis());
        if (!ok) {
            Log.w(TAG, "Failed to set last-modified date on " + file);
        }//from  w ww  . j  av a 2 s.co  m
    }
}

From source file:com.facebook.buck.util.unarchive.Untar.java

/** Sets the modification time and the execution bit on a file */
private void setAttributes(ProjectFilesystem filesystem, Path path, TarArchiveEntry entry) throws IOException {
    Path filePath = filesystem.getRootPath().resolve(path);
    File file = filePath.toFile();
    file.setLastModified(entry.getModTime().getTime());
    Set<PosixFilePermission> posixPermissions = MorePosixFilePermissions.fromMode(entry.getMode());
    if (posixPermissions.contains(PosixFilePermission.OWNER_EXECUTE)) {
        // setting posix file permissions on a symlink does not work, so use File API instead
        if (entry.isSymbolicLink()) {
            file.setExecutable(true, true);
        } else {/*w w w.  j  av a  2 s.  c o  m*/
            MostFiles.makeExecutable(filePath);
        }
    }
}

From source file:gobblin.data.management.copy.recovery.RecoveryHelperTest.java

@Test
public void testPurge() throws Exception {
    String content = "contents";

    File persistDirBase = Files.createTempDir();
    persistDirBase.deleteOnExit();//from   ww  w.  j a  v a 2 s  .co m

    State state = new State();
    state.setProp(RecoveryHelper.PERSIST_DIR_KEY, persistDirBase.getAbsolutePath());
    state.setProp(RecoveryHelper.PERSIST_RETENTION_KEY, "1");

    RecoveryHelper recoveryHelper = new RecoveryHelper(FileSystem.getLocal(new Configuration()), state);
    File persistDir = new File(RecoveryHelper.getPersistDir(state).get().toString());
    persistDir.mkdir();

    File file = new File(persistDir, "file1");
    OutputStream os = new FileOutputStream(file);
    IOUtils.write(content, os);
    os.close();
    file.setLastModified(System.currentTimeMillis() - TimeUnit.HOURS.toMillis(2));

    File file2 = new File(persistDir, "file2");
    OutputStream os2 = new FileOutputStream(file2);
    IOUtils.write(content, os2);
    os2.close();

    Assert.assertEquals(persistDir.listFiles().length, 2);

    recoveryHelper.purgeOldPersistedFile();

    Assert.assertEquals(persistDir.listFiles().length, 1);
}

From source file:com.alibaba.antx.util.ZipUtil.java

/**
 * //from   ww w  . j a  va2  s  . c o m
 *
 * @param todir     
 * @param zipStream ?
 * @param zipEntry  zip
 * @param overwrite ?
 * @throws IOException Zip?
 */
protected static void extractFile(File todir, InputStream zipStream, ZipEntry zipEntry, boolean overwrite)
        throws IOException {
    String entryName = zipEntry.getName();
    Date entryDate = new Date(zipEntry.getTime());
    boolean isDirectory = zipEntry.isDirectory();
    File targetFile = FileUtil.getFile(todir, entryName);

    if (!overwrite && targetFile.exists() && targetFile.lastModified() >= entryDate.getTime()) {
        log.debug("Skipping " + targetFile + " as it is up-to-date");
        return;
    }

    log.info("expanding " + entryName + " to " + targetFile);

    if (isDirectory) {
        targetFile.mkdirs();
    } else {
        File dir = targetFile.getParentFile();

        dir.mkdirs();

        byte[] buffer = new byte[8192];
        int length = 0;
        OutputStream ostream = null;

        try {
            ostream = new BufferedOutputStream(new FileOutputStream(targetFile), 8192);

            while ((length = zipStream.read(buffer)) >= 0) {
                ostream.write(buffer, 0, length);
            }
        } finally {
            if (ostream != null) {
                try {
                    ostream.close();
                } catch (IOException e) {
                }
            }
        }
    }

    targetFile.setLastModified(entryDate.getTime());
}