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:org.ngrinder.common.util.CompressionUtil.java

/**
 * Unpack the given jar file./*from w ww .ja  v  a2s . c o  m*/
 * 
 * @param jarFile
 *            file to be uncompressed
 * @param destDir
 *            destination directory
 * @throws IOException
 *             occurs when IO has a problem.
 */
public static void unjar(File jarFile, String destDir) throws IOException {
    File dest = new File(destDir);
    if (!dest.exists()) {
        dest.mkdirs();
    }
    if (!dest.isDirectory()) {
        LOGGER.error("Destination must be a directory.");
        throw new IOException("Destination must be a directory.");
    }
    JarInputStream jin = new JarInputStream(new FileInputStream(jarFile));
    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();
            IOUtils.closeQuietly(out);
            jin.closeEntry();
            file.setLastModified(entry.getTime());
        }
        entry = jin.getNextEntry();
    }
    Manifest mf = jin.getManifest();
    if (mf != null) {
        File file = new File(dest, "META-INF/MANIFEST.MF");
        File parent = file.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        OutputStream out = new FileOutputStream(file);
        mf.write(out);
        out.flush();
        IOUtils.closeQuietly(out);
    }
    IOUtils.closeQuietly(jin);
}

From source file:com.ibm.jaggr.core.util.ZipUtilTest.java

@Before
public void setUp() throws Exception {
    tmpdir = Files.createTempDir();
    sourceDir = new File(tmpdir, "source");
    targetDir = new File(tmpdir, "target");
    sourceDir.mkdir();/*from  w  ww .j av a  2s. c o m*/
    targetDir.mkdir();
    targetDir.setLastModified(sourceDir.lastModified());
    Files.write(ROOT_FILE_CONTENTS, new File(sourceDir, "rootFile.txt"), Charsets.UTF_8);
    File rootDir = new File(sourceDir, "rootDir");
    File subDir = new File(rootDir, "subDir");
    rootDir.mkdir();
    subDir.mkdir();
    File file1 = new File(rootDir, "file1");
    File file2 = new File(subDir, "file2");
    Files.write(FILE1_CONTENTS, file1, Charsets.UTF_8);
    Files.write(FILE2_CONTENTS, file2, Charsets.UTF_8);
    file1.setLastModified(file1.lastModified() - 10000);
    subDir.setLastModified(subDir.lastModified() + 120000);
}

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

/** Set the modification times on directories that were directly specified in the archive */
private void setDirectoryModificationTimes(ProjectFilesystem filesystem, NavigableMap<Path, Long> dirToTime) {
    for (Map.Entry<Path, Long> pathAndTime : dirToTime.descendingMap().entrySet()) {
        File file = filesystem.getRootPath().resolve(pathAndTime.getKey()).toFile();
        file.setLastModified(pathAndTime.getValue());
    }/*from w  w w .  j a  v  a2s . c  o m*/
}

From source file:org.carewebframework.maven.plugin.core.BaseMojo.java

/**
 * Creates a new file in the staging directory. Ensures that all folders in the path are also
 * created.//  w w  w  .ja va 2s  .  c om
 * 
 * @param entryName Entry name to create.
 * @param modTime Modification timestamp for the new entry. If 0, defaults to the current time.
 * @return the new file
 */
public File newStagingFile(String entryName, long modTime) {
    File file = new File(stagingDirectory, entryName);

    if (modTime != 0) {
        file.setLastModified(modTime);
    }

    file.getParentFile().mkdirs();
    return file;
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.file.FileTest.java

/**
 * @throws Exception if the test fails/* w  w w .ja  va 2s. c om*/
 */
@Test
@Alerts(CHROME = { "1", "ScriptExceptionTest1.txt",
        "Sun Jul 26 2015 16:21:47 GMT+0200 (Central European Summer Time)", "1437920507000", "", "14",
        "text/plain" }, FF31 = { "1", "ScriptExceptionTest1.txt", "Sun Jul 26 2015 16:21:47 GMT+0200",
                "undefined", "undefined", "14", "text/plain" }, FF38 = { "1", "ScriptExceptionTest1.txt",
                        "Sun Jul 26 2015 16:21:47 GMT+0200", "1437920507000", "undefined", "14",
                        "text/plain" }, IE = { "1", "ScriptExceptionTest1.txt",
                                "Sun Jul 26 2015 16:21:47 GMT+0200 (Central European Summer Time)", "undefined",
                                "undefined", "14", "text/plain" })
public void properties() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html>\n" + "<head><title>foo</title>\n"
            + "<script>\n" + "function test() {\n" + "  if (document.testForm.fileupload.files) {\n"
            + "    var files = document.testForm.fileupload.files;\n" + "    alert(files.length);\n"

            + "    var file = files[0];\n" + "    alert(file.name);\n" + "    alert(file.lastModifiedDate);\n"
            + "    alert(file.lastModified);\n" + "    alert(file.webkitRelativePath);\n"
            + "    alert(file.size);\n" + "    alert(file.type);\n" + "  }\n" + "}\n" + "</script>\n"
            + "</head>\n" + "<body>\n" + "  <form name='testForm'>\n"
            + "    <input type='file' id='fileupload' name='fileupload'>\n" + "  </form>\n"
            + "  <button id='testBtn' onclick='test()'>Tester</button>\n" + "</body>\n" + "</html>";

    final WebDriver driver = loadPage2(html);

    final File tstFile = File.createTempFile("HtmlUnitUploadTest", ".txt");
    try {
        FileUtils.writeStringToFile(tstFile, "Hello HtmlUnit");

        // do not use millis here because different file systems
        // have different precisions
        tstFile.setLastModified(1437920507000L);

        final String path = tstFile.getCanonicalPath();
        driver.findElement(By.name("fileupload")).sendKeys(path);

        driver.findElement(By.id("testBtn")).click();

        final String[] expected = getExpectedAlerts();
        if (expected.length > 1) {
            expected[1] = tstFile.getName();
        }

        verifyAlerts(driver, getExpectedAlerts());
    } finally {
        FileUtils.deleteQuietly(tstFile);
    }
}

From source file:FileHelper.java

private static void copyFile(File srcFile, File destFile, long chunkSize) throws IOException {
    FileInputStream is = null;/*from   w ww  .j a  va  2 s  . c  o  m*/
    FileOutputStream os = null;
    try {
        is = new FileInputStream(srcFile);
        FileChannel iChannel = is.getChannel();
        os = new FileOutputStream(destFile, false);
        FileChannel oChannel = os.getChannel();
        long doneBytes = 0L;
        long todoBytes = srcFile.length();
        while (todoBytes != 0L) {
            long iterationBytes = Math.min(todoBytes, chunkSize);
            long transferredLength = oChannel.transferFrom(iChannel, doneBytes, iterationBytes);
            if (iterationBytes != transferredLength) {
                throw new IOException("Error during file transfer: expected " + iterationBytes + " bytes, only "
                        + transferredLength + " bytes copied.");
            }
            doneBytes += transferredLength;
            todoBytes -= transferredLength;
        }
    } finally {
        if (is != null) {
            is.close();
        }
        if (os != null) {
            os.close();
        }
    }
    boolean successTimestampOp = destFile.setLastModified(srcFile.lastModified());
    if (!successTimestampOp) {
        System.out.println("Could not change timestamp for {}. Index synchronization may be slow. " + destFile);
    }
}

From source file:org.jvnet.hudson.plugins.thinbackup.backup.TestHudsonBackup.java

public void performHudsonDiffBackup(final ThinBackupPluginImpl mockPlugin) throws Exception {
    final Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE - 10));

    new HudsonBackup(mockPlugin, BackupType.FULL, cal.getTime(), mockHudson).backup();

    // fake modification
    backupDir.listFiles((FileFilter) FileFilterUtils.prefixFileFilter(BackupType.FULL.toString()))[0]
            .setLastModified(System.currentTimeMillis() - 60000 * 60);

    for (final File globalConfigFile : jenkinsHome.listFiles()) {
        globalConfigFile.setLastModified(System.currentTimeMillis() - 60000 * 120);
    }//from  ww  w .  j  a v  a  2  s  .  co m

    new HudsonBackup(mockPlugin, BackupType.DIFF, new Date(), mockHudson).backup();
}

From source file:com.norconex.collector.http.crawler.ExecutionTest.java

private void ageProgress(File progressDir) {
    final long age = System.currentTimeMillis() - (10 * 1000);
    FileUtil.visitAllFiles(progressDir, new IFileVisitor() {
        @Override//from   w w  w . j a va  2s  .co m
        public void visit(File file) {
            file.setLastModified(age);
        }
    });
}

From source file:nl.strohalm.cyclos.utils.customizedfile.BaseCustomizedFileHandler.java

protected final synchronized void writeLocally(final String path, final long lastModified,
        final byte[] contents) {
    final String realPath = context.getRealPath(path);
    final File file = new File(realPath);
    file.getParentFile().mkdirs();/*from ww  w. j  a  v a2s  .c om*/
    if (file.exists()) {
        file.delete();
    }
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(file);
        out.write(contents, 0, contents.length);
        file.setLastModified(lastModified);
        LOG.debug("Wrote local file " + file);
    } catch (final Exception e) {
        LOG.error("Error writing local file", e);
    }
}

From source file:org.rhq.enterprise.gui.installer.server.servlet.ServerInstallUtil.java

public static void touchMarkerFile(String dir, String artifact, Marker marker) throws Exception {
    File markerFile = getMarkerFile(dir, artifact, marker);
    markerFile.createNewFile();//from w w w  . j  av a 2  s  .c o m
    markerFile.setLastModified(System.currentTimeMillis());
}