Example usage for java.nio.file Files setLastModifiedTime

List of usage examples for java.nio.file Files setLastModifiedTime

Introduction

In this page you can find the example usage for java.nio.file Files setLastModifiedTime.

Prototype

public static Path setLastModifiedTime(Path path, FileTime time) throws IOException 

Source Link

Document

Updates a file's last modified time attribute.

Usage

From source file:com.facebook.buck.io.filesystem.impl.DefaultProjectFilesystemTest.java

@Test
public void testSortedDirectoryContents() throws IOException {
    tmp.newFolder("foo");
    Path a = tmp.newFile("foo/a.txt");
    Files.setLastModifiedTime(a, FileTime.fromMillis(1000));
    Path b = tmp.newFile("foo/b.txt");
    Files.setLastModifiedTime(b, FileTime.fromMillis(2000));
    Path c = tmp.newFile("foo/c.txt");
    Files.setLastModifiedTime(c, FileTime.fromMillis(3000));
    tmp.newFile("foo/non-matching");

    assertEquals(ImmutableSet.of(c, b, a),
            filesystem.getMtimeSortedMatchingDirectoryContents(Paths.get("foo"), "*.txt"));
}

From source file:org.abysm.onionzip.ZipFileExtractHelper.java

void extractZipArchiveEntries() throws IOException {
    ZipFile zipFile = new ZipFile(zipFilename, encoding);
    try {/*from   w ww . ja v  a2  s. c o  m*/
        for (Enumeration<ZipArchiveEntry> zipArchiveEntryEnumeration = zipFile
                .getEntries(); zipArchiveEntryEnumeration.hasMoreElements();) {
            ZipArchiveEntry entry = zipArchiveEntryEnumeration.nextElement();
            System.out.println(entry.getName());
            if (entry.isDirectory()) {
                Path directory = Paths.get(entry.getName());
                Files.createDirectories(directory);
            } else if (entry.isUnixSymlink()) {
                Path symlink = Paths.get(entry.getName());
                Path parentDirectory = symlink.getParent();
                Path target = Paths.get(zipFile.getUnixSymlink(entry));
                if (parentDirectory != null) {
                    Files.createDirectories(parentDirectory);
                }
                Files.createSymbolicLink(symlink, target);
            } else {
                Path file = Paths.get(entry.getName());
                Path parentDirectory = file.getParent();
                if (parentDirectory != null) {
                    Files.createDirectories(parentDirectory);
                }
                InputStream contentInputStream = zipFile.getInputStream(entry);
                FileOutputStream extractedFileOutputStream = new FileOutputStream(entry.getName());
                try {
                    IOUtils.copy(contentInputStream, extractedFileOutputStream);
                } finally {
                    IOUtils.closeQuietly(contentInputStream);
                    IOUtils.closeQuietly(extractedFileOutputStream);
                }
                FileTime fileTime = FileTime.fromMillis(entry.getLastModifiedDate().getTime());
                Files.setLastModifiedTime(file, fileTime);
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
}

From source file:org.apache.solr.util.UtilsToolTest.java

@Test
public void testRemoveOldSolrLogs() throws Exception {
    String[] args = { "utils", "-remove_old_solr_logs", "1", "-l", dir.toString() };
    assertEquals(files.size(), fileCount());
    assertEquals(0, runTool(args));//from www.ja  v a2s .  c  o  m
    assertEquals(files.size(), fileCount()); // No logs older than 1 day
    Files.setLastModifiedTime(dir.resolve("solr_log_20160102"),
            FileTime.from(Instant.now().minus(Period.ofDays(2))));
    assertEquals(0, runTool(args));
    assertEquals(files.size() - 1, fileCount()); // One logs older than 1 day
    Files.setLastModifiedTime(dir.resolve("solr_log_20160304"),
            FileTime.from(Instant.now().minus(Period.ofDays(3))));
    assertEquals(0, runTool(args));
    assertEquals(files.size() - 2, fileCount()); // Two logs older than 1 day
}

From source file:org.fim.command.ResetFileAttributesCommand.java

private boolean resetLastModified(Path file, FileState fileState, BasicFileAttributes attributes)
        throws IOException {
    long lastModified = attributes.lastModifiedTime().toMillis();
    long previousLastModified = fileState.getFileTime().getLastModified();
    if (lastModified != previousLastModified) {
        Files.setLastModifiedTime(file, FileTime.fromMillis(previousLastModified));
        System.out.printf("Set last modified: %s \t%s -> %s%n", fileState.getFileName(),
                formatDate(lastModified), formatDate(previousLastModified));
        return true;
    }/*from   w w w  .  ja v a 2  s  . co  m*/
    return false;
}

From source file:org.fim.tooling.RepositoryTool.java

public void touchLastModified(String fileName) throws IOException {
    Path file = rootDir.resolve(fileName);
    long timeStamp = Math.max(System.currentTimeMillis(), Files.getLastModifiedTime(file).toMillis());
    timeStamp += 1_000;//w ww.j  a  v  a  2s .  c om
    Files.setLastModifiedTime(file, FileTime.fromMillis(timeStamp));
}

From source file:org.mycore.common.MCRUtils.java

/**
 * Extracts files in a tar archive. Currently works only on uncompressed tar files.
 * // w  w  w .j a va 2 s . co  m
 * @param source
 *            the uncompressed tar to extract
 * @param expandToDirectory
 *            the directory to extract the tar file to
 * @throws IOException
 *             if the source file does not exists
 */
public static void untar(Path source, Path expandToDirectory) throws IOException {
    try (TarArchiveInputStream tain = new TarArchiveInputStream(Files.newInputStream(source))) {
        TarArchiveEntry tarEntry;
        FileSystem targetFS = expandToDirectory.getFileSystem();
        HashMap<Path, FileTime> directoryTimes = new HashMap<>();
        while ((tarEntry = tain.getNextTarEntry()) != null) {
            Path target = MCRPathUtils.getPath(targetFS, tarEntry.getName());
            Path absoluteTarget = expandToDirectory.resolve(target).normalize().toAbsolutePath();
            if (tarEntry.isDirectory()) {
                Files.createDirectories(expandToDirectory.resolve(absoluteTarget));
                directoryTimes.put(absoluteTarget,
                        FileTime.fromMillis(tarEntry.getLastModifiedDate().getTime()));
            } else {
                if (Files.notExists(absoluteTarget.getParent())) {
                    Files.createDirectories(absoluteTarget.getParent());
                }
                Files.copy(tain, absoluteTarget, StandardCopyOption.REPLACE_EXISTING);
                Files.setLastModifiedTime(absoluteTarget,
                        FileTime.fromMillis(tarEntry.getLastModifiedDate().getTime()));
            }
        }
        //restore directory dates
        Files.walkFileTree(expandToDirectory, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Path absolutePath = dir.normalize().toAbsolutePath();
                Files.setLastModifiedTime(absolutePath, directoryTimes.get(absolutePath));
                return super.postVisitDirectory(dir, exc);
            }
        });
    }
}

From source file:org.panbox.desktop.common.vfs.backend.generic.GenericVirtualFileImpl.java

@Override
public void setModifiedTime(long mtime) throws IOException {
    if (mtime > 0) {
        FileTime fileTime = FileTime.fromMillis(mtime);
        Files.setLastModifiedTime(file.toPath(), fileTime);
    }// w  w  w. j  a v  a2s.c o  m
}

From source file:org.sakuli.starter.helper.SahiProxyTest.java

@Test
public void testInjectCustomJavaScriptFilesOverridingNewer() throws Exception {
    Path configOrig = Paths.get(this.getClass().getResource("mock-files/inject_top.txt").toURI());
    Path config = Paths.get(configOrig.getParent().toString() + File.separator + "inject_top_temp.txt");
    Path targetOrig = Paths.get(this.getClass().getResource("mock-old-js/inject.js").toURI());
    Path target = Paths.get(targetOrig.getParent().toString() + File.separator + "inject_temp.js");
    FileUtils.copyFile(configOrig.toFile(), config.toFile());
    FileUtils.copyFile(targetOrig.toFile(), target.toFile());

    Files.setLastModifiedTime(target, FileTime.fromMillis(DateTime.now().minusYears(10).getMillis()));
    Path source = Paths.get(SahiProxy.class.getResource("inject_source.js").toURI());

    when(props.getSahiJSInjectConfigFile()).thenReturn(config);
    when(props.getSahiJSInjectTargetFile()).thenReturn(target);
    when(props.getSahiJSInjectSourceFile()).thenReturn(source);
    assertFalse(FileUtils.readFileToString(config.toFile()).contains(SahiProxy.SAKULI_INJECT_SCRIPT_TAG));
    assertContains(FileUtils.readFileToString(target.toFile()), "//old inject.js");

    testling.injectCustomJavaScriptFiles();
    assertTrue(FileUtils.readFileToString(config.toFile()).contains(SahiProxy.SAKULI_INJECT_SCRIPT_TAG));
    assertContains(FileUtils.readFileToString(target.toFile()),
            "Sahi.prototype.originalEx = Sahi.prototype.ex;");
    assertContains(FileUtils.readFileToString(target.toFile()), "Sahi.prototype.ex = function (isStep) {");

}

From source file:org.syncany.operations.down.actions.FileSystemAction.java

protected void setLastModified(FileVersion reconstructedFileVersion, File reconstructedFilesAtFinalLocation) {
    // Using Files.setLastModifiedTime() instead of File.setLastModified()  
    // due to pre-1970 issue. See #374 for details.

    try {//from w w  w. j av  a2  s  . c  o  m
        FileTime newLastModifiedTime = FileTime
                .fromMillis(reconstructedFileVersion.getLastModified().getTime());
        Files.setLastModifiedTime(reconstructedFilesAtFinalLocation.toPath(), newLastModifiedTime);
    } catch (IOException e) {
        logger.log(Level.WARNING, "Warning: Could not set last modified date for file "
                + reconstructedFilesAtFinalLocation + "; Ignoring error.", e);
    }
}

From source file:stroom.util.test.StroomTestUtil.java

/**
 * Similar to the unix touch cammand. Sets the last modified time to now if the file
 * exists else, creates the file// ww w . j  a  va 2s  .c  o  m
 *
 * @param file
 * @throws IOException
 */
public static void touchFile(Path file) throws IOException {

    if (Files.exists(file)) {
        if (!Files.isRegularFile(file)) {
            throw new RuntimeException(
                    String.format("File %s is not a regular file", file.toAbsolutePath().toString()));
        }
        try {
            Files.setLastModifiedTime(file, FileTime.from(Instant.now()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        Files.createFile(file);
    }
}