Example usage for java.nio.file.attribute FileTime from

List of usage examples for java.nio.file.attribute FileTime from

Introduction

In this page you can find the example usage for java.nio.file.attribute FileTime from.

Prototype

public static FileTime from(Instant instant) 

Source Link

Document

Returns a FileTime representing the same point of time value on the time-line as the provided Instant object.

Usage

From source file:Main.java

public static void main(String[] args) {
    Path path = Paths.get("C:\\Java_Dev\\test1.txt");

    try {/*  ww  w. j a va 2  s  .co  m*/
        BasicFileAttributeView bfv = Files.getFileAttributeView(path, BasicFileAttributeView.class);
        BasicFileAttributes bfa = bfv.readAttributes();

        System.out.format("Size:%s bytes %n", bfa.size());
        System.out.format("Creation  Time:%s %n", bfa.creationTime());
        System.out.format("Last Access  Time:%s %n", bfa.lastAccessTime());

        FileTime newLastModifiedTime = null;
        FileTime newLastAccessTime = null;
        FileTime newCreateTime = FileTime.from(Instant.now());

        bfv.setTimes(newLastModifiedTime, newLastAccessTime, newCreateTime);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:be.neutrinet.ispng.vpn.api.VPNClientConfig.java

protected Representation finalizeZip(List<String> config, ZipOutputStream zip, ByteArrayOutputStream baos)
        throws Exception {
    String file = "";
    for (String s : config) {
        file += s + '\n';
    }/*from   w  w  w. java2  s  .c  o m*/

    if (caCert == null) {
        caCert = IOUtils.toByteArray(new FileInputStream(VPN.cfg.getProperty("ca.crt")));
    }

    ZipEntry configFile = new ZipEntry("neutrinet.ovpn");
    configFile.setCreationTime(FileTime.from(Instant.now()));
    zip.putNextEntry(configFile);
    zip.write(file.getBytes());
    zip.putNextEntry(new ZipEntry("ca.crt"));
    zip.write(caCert);

    zip.close();

    ByteArrayRepresentation rep = new ByteArrayRepresentation(baos.toByteArray());
    rep.setMediaType(MediaType.APPLICATION_ZIP);
    rep.setSize(baos.size());
    rep.setCharacterSet(CharacterSet.UTF_8);
    rep.setDisposition(new Disposition(Disposition.TYPE_ATTACHMENT));
    return rep;
}

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));// w  ww  .jav a  2s.  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.cryptomator.frontend.webdav.servlet.DavNode.java

protected void setModificationTime(Instant instant) throws DavException {
    BasicFileAttributeView attrView = Files.getFileAttributeView(path, BasicFileAttributeView.class);
    if (attrView != null) {
        try {//from  w w  w . j  a  v  a 2 s  .  c  om
            attrView.setTimes(FileTime.from(instant), null, null);
        } catch (IOException e) {
            throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, e);
        }
    }
}

From source file:org.cryptomator.frontend.webdav.servlet.DavNode.java

protected void setCreationTime(Instant instant) throws DavException {
    BasicFileAttributeView attrView = Files.getFileAttributeView(path, BasicFileAttributeView.class);
    if (attrView != null) {
        try {//from   ww w .java2 s. co m
            attrView.setTimes(null, null, FileTime.from(instant));
        } catch (IOException e) {
            throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_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//from w w  w  .ja v a  2 s  . c om
 *
 * @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);
    }
}