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.FunctionalDownloadTest.java

/**
 * Download a file once, then download again with 'onlyIfNewer'.
 * File changed between downloads./* w  ww .  java 2s  . c  o  m*/
 * @throws Exception if anything went wrong
 */
@Test
public void downloadOnlyIfNewerRedownloadsIfFileHasBeenUpdated() throws Exception {
    assertTaskSuccess(download(new Parameters(singleSrc, dest, false, false)));
    File src = new File(folder.getRoot(), TEST_FILE_NAME);
    assertTrue(src.setLastModified(src.lastModified() + 5000));
    assertTaskSuccess(download(new Parameters(singleSrc, dest, true, true)));
}

From source file:gov.nih.nci.restgen.util.JarHelper.java

/**
 * Given an InputStream on a jar file, unjars the contents into the given
 * directory./* w w w  .  ja va  2  s .com*/
 */
public void unjar(InputStream in, File destDir) throws IOException {
    BufferedOutputStream dest = null;
    JarInputStream jis = new JarInputStream(in);
    JarEntry entry;
    while ((entry = jis.getNextJarEntry()) != null) {
        if (entry.isDirectory()) {
            File dir = new File(destDir, entry.getName());
            dir.mkdir();
            if (entry.getTime() != -1)
                dir.setLastModified(entry.getTime());
            continue;
        }
        int count;
        byte data[] = new byte[BUFFER_SIZE];
        File destFile = new File(destDir, entry.getName());
        if (mVerbose) {
            //System.out.println("unjarring " + destFile + " from " + entry.getName());
        }
        FileOutputStream fos = new FileOutputStream(destFile);
        dest = new BufferedOutputStream(fos, BUFFER_SIZE);
        while ((count = jis.read(data, 0, BUFFER_SIZE)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
        if (entry.getTime() != -1)
            destFile.setLastModified(entry.getTime());
    }
    jis.close();
}

From source file:com.altoukhov.svsync.fileviews.LocalFileSpace.java

private boolean setFileTimestamp(String path, DateTime timestamp) {

    try {/*from   ww w. j a  v  a2 s  .  c o m*/
        File file = new File(toAbsolutePath(path));
        return file.setLastModified(timestamp.toDate().getTime());
    } catch (Exception ex) {
        System.out.println("Failed to set file's original timestamp: " + ex.getMessage());
        return false;
    }
}

From source file:org.apache.oozie.util.TestOozieRollingPolicy.java

private void _testDeletingOldFiles(String oozieLogName, int calendarUnit) throws IOException {
    // OozieRollingPolicy gets the log path and log name from XLogService by calling Services.get.get(XLogService.class) so we
    // use a mock version where we overwrite the XLogService.getOozieLogName() and XLogService.getOozieLogPath() to simply 
    // return these values instead of involving Services.  We then overwrite OozieRollingPolicy.getXLogService() to return the 
    // mock one instead.
    String oozieLogPath = getTestCaseDir();

    OozieRollingPolicy orp = new OozieRollingPolicy();

    if (calendarUnit == Calendar.DAY_OF_MONTH) {
        orp.setFileNamePattern(oozieLogPath + "/" + oozieLogName + "-%d{yyyy-MM-dd}");
    } else {//from   ww w  .  j  a va  2s.co m
        orp.setFileNamePattern(oozieLogPath + "/" + oozieLogName + "-%d{yyyy-MM-dd-HH}");

    }
    orp.setMaxHistory(3); // only keep 3 newest logs

    Calendar cal = new GregorianCalendar();
    final File f0 = new File(oozieLogPath, oozieLogName);
    f0.createNewFile();
    f0.setLastModified(cal.getTimeInMillis());
    cal.add(calendarUnit, 1);
    final File f1 = new File(oozieLogPath, oozieLogName + formatDateForFilename(cal, calendarUnit) + ".gz");
    f1.createNewFile();
    cal.add(calendarUnit, 1);
    final File f2 = new File(oozieLogPath, oozieLogName + formatDateForFilename(cal, calendarUnit) + ".gz");
    f2.createNewFile();
    cal.add(calendarUnit, 1);
    final File f3 = new File(oozieLogPath, oozieLogName + formatDateForFilename(cal, calendarUnit) + ".gz");
    f3.createNewFile();
    cal.add(calendarUnit, 1);
    final File f4 = new File(oozieLogPath, oozieLogName + formatDateForFilename(cal, calendarUnit) + ".gz");
    f4.createNewFile();

    // Test that it only deletes the oldest file (f1)
    orp.isTriggeringEvent(null, null, null, 0);
    waitFor(60 * 1000, new Predicate() {
        @Override
        public boolean evaluate() throws Exception {
            return (f0.exists() && !f1.exists() && f2.exists() && f3.exists() && f4.exists());
        }
    });
    assertTrue(f0.exists() && !f1.exists() && f2.exists() && f3.exists() && f4.exists());

    cal.add(calendarUnit, 1);
    final File f5 = new File(oozieLogPath, oozieLogName + formatDateForFilename(cal, calendarUnit));
    f5.createNewFile();
    f5.setLastModified(cal.getTimeInMillis());

    cal.add(calendarUnit, -15);
    final File f6 = new File(oozieLogPath, oozieLogName + formatDateForFilename(cal, calendarUnit));
    f6.createNewFile();
    f6.setLastModified(cal.getTimeInMillis());

    // Test that it can delete more than one file when necessary and that it works with non .gz files
    orp.isTriggeringEvent(null, null, null, 0);
    waitFor(60 * 1000, new Predicate() {
        @Override
        public boolean evaluate() throws Exception {
            return (f0.exists() && !f1.exists() && !f2.exists() && f3.exists() && f4.exists() && f5.exists()
                    && !f6.exists());
        }
    });
    assertTrue(f0.exists() && !f1.exists() && !f2.exists() && f3.exists() && f4.exists() && f5.exists()
            && !f6.exists());

    final File f7 = new File(oozieLogPath, "blah.txt");
    f7.createNewFile();
    f7.setLastModified(cal.getTimeInMillis());

    cal.add(calendarUnit, 1);
    final File f8 = new File(oozieLogPath, oozieLogName + formatDateForFilename(cal, calendarUnit));
    cal.add(calendarUnit, 15);
    f8.createNewFile();
    f8.setLastModified(cal.getTimeInMillis());

    // Test that it ignores "other" files even if they are oldest and test that it uses the modified time for non .gz files 
    // (instead of the time from the filename)
    orp.isTriggeringEvent(null, null, null, 0);
    waitFor(60 * 1000, new Predicate() {
        @Override
        public boolean evaluate() throws Exception {
            return (f0.exists() && !f1.exists() && !f2.exists() && !f3.exists() && f4.exists() && f5.exists()
                    && !f6.exists() && f7.exists() && f8.exists());
        }
    });
    assertTrue(f0.exists() && !f1.exists() && !f2.exists() && !f3.exists() && f4.exists() && f5.exists()
            && !f6.exists() && f7.exists() && f8.exists());
}

From source file:org.eclipse.jdt.ls.core.internal.managers.MavenProjectImporterTest.java

@Test
public void testChangedProjectShouldBeUpdated() throws Exception {
    attachJobSpy();//ww  w. j a  v a  2s.c o  m
    String name = "salut";
    IProject salut = importMavenProject(name);
    assertEquals("New Project should not be updated", 0, jobSpy.updateProjectJobCalled);
    File pom = salut.getFile(MavenProjectImporter.POM_FILE).getRawLocation().toFile();
    pom.setLastModified(System.currentTimeMillis() + 1000);
    importExistingMavenProject(name);
    assertEquals("Changed Project should be updated", 1, jobSpy.updateProjectJobCalled);
}

From source file:org.eclipse.thym.core.internal.util.FileUtils.java

public static File[] untarFile(File source, File outputDir) throws IOException, TarException {
    TarFile tarFile = new TarFile(source);
    List<File> untarredFiles = new ArrayList<File>();
    try {//from   ww  w. j a va  2s.c  o  m
        for (Enumeration<TarEntry> e = tarFile.entries(); e.hasMoreElements();) {
            TarEntry entry = e.nextElement();
            InputStream input = tarFile.getInputStream(entry);
            try {
                File outFile = new File(outputDir, entry.getName());
                outFile = outFile.getCanonicalFile(); //bug 266844
                untarredFiles.add(outFile);
                if (entry.getFileType() == TarEntry.DIRECTORY) {
                    outFile.mkdirs();
                } else {
                    if (outFile.exists())
                        outFile.delete();
                    else
                        outFile.getParentFile().mkdirs();
                    try {
                        copyStream(input, false, new FileOutputStream(outFile), true);
                    } catch (FileNotFoundException e1) {
                        // TEMP: ignore this for now in case we're trying to replace
                        // a running eclipse.exe
                    }
                    outFile.setLastModified(entry.getTime());
                }
            } finally {
                input.close();
            }
        }
    } finally {
        tarFile.close();
    }
    return untarredFiles.toArray(new File[untarredFiles.size()]);
}

From source file:com.kylinolap.common.persistence.FileResourceStore.java

@Override
protected void putResourceImpl(String resPath, InputStream content, long ts) throws IOException {
    File f = file(resPath);
    f.getParentFile().mkdirs();//from   ww  w . ja v  a  2s  . c  o m
    FileOutputStream out = new FileOutputStream(f);
    try {
        IOUtils.copy(content, out);
    } finally {
        IOUtils.closeQuietly(out);
    }

    f.setLastModified(ts);
}

From source file:org.eclipse.flux.cloudfoundry.deployment.service.DownloadProject.java

public void getResourceResponse(JSONObject response) {
    try {/*from ww w  . j av a 2 s.c  om*/
        final String responseUser = response.getString("username");
        final String resourcePath = response.getString("resource");
        final long timestamp = response.getLong("timestamp");
        final String content = response.getString("content");

        System.out.println("resource-response: " + response);

        if (this.username.equals(responseUser)) {
            File file = new File(project, resourcePath);
            IOUtil.pipe(new ByteArrayInputStream(content.getBytes()), file);
            file.setLastModified(timestamp);

            int downloaded = this.downloadedFileCount.incrementAndGet();
            if (downloaded == this.requestedFileCount.get()) {
                this.messagingConnector.removeMessageHandler(projectResponseHandler);
                this.messagingConnector.removeMessageHandler(resourceResponseHandler);
                this.completionCallback.downloadComplete(project);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        this.messagingConnector.removeMessageHandler(projectResponseHandler);
        this.messagingConnector.removeMessageHandler(resourceResponseHandler);
        this.completionCallback.downloadFailed();
    }
}

From source file:org.gradle.api.internal.file.CopyVisitor.java

void copyFile(File srcFile, File destFile) throws IOException {
    didWork = true;/*from   w ww  . ja  v  a  2 s . c  o m*/
    if (filterChain.hasFilters()) {
        copyFileFiltered(srcFile, destFile);
    } else {
        copyFileStreams(srcFile, destFile);
    }
    destFile.setLastModified(srcFile.lastModified());
}

From source file:org.eclipse.tycho.plugins.tar.TarGzArchiverTest.java

@Before
public void createTestFiles() throws Exception {
    archiver = new TarGzArchiver();
    tarGzArchive = tempFolder.newFile("test.tar.gz");
    archiver.setDestFile(tarGzArchive);//w w  w .jav a  2  s  .c om
    this.archiveRoot = tempFolder.newFolder("dir1");
    File dir2 = new File(archiveRoot, "dir2");
    assertTrue(dir2.mkdirs());
    archiver.addDirectory(archiveRoot);
    File textFile = new File(dir2, "test.txt");
    assertTrue(textFile.createNewFile());
    FileUtils.fileWrite(textFile, "hello");
    File dir3 = new File(dir2, "dir3");
    assertTrue(dir3.mkdirs());
    assertTrue(new File(dir3, "test.sh").createNewFile());
    this.testPermissionsFile = new File(dir2, "testPermissions");
    assertTrue(testPermissionsFile.createNewFile());
    File testLastModifiedFile = new File(dir2, "testLastModified");
    assertTrue(testLastModifiedFile.createNewFile());
    testLastModifiedFile.setLastModified(0L);
    this.testOwnerAndGroupFile = new File(dir2, "testOwnerAndGroupName");
    assertTrue(testOwnerAndGroupFile.createNewFile());
}