Example usage for java.nio.file.attribute BasicFileAttributes creationTime

List of usage examples for java.nio.file.attribute BasicFileAttributes creationTime

Introduction

In this page you can find the example usage for java.nio.file.attribute BasicFileAttributes creationTime.

Prototype

FileTime creationTime();

Source Link

Document

Returns the creation time.

Usage

From source file:de.bitinsomnia.webdav.server.MiltonFileResource.java

@Override
public Date getCreateDate() {
    Path filePath = Paths.get(this.file.toURI());
    try {//from   w  ww.  j a  v  a  2s . c  om
        BasicFileAttributes attr = Files.readAttributes(filePath, BasicFileAttributes.class);
        long creationTimeMillis = attr.creationTime().toMillis();
        return new Date(creationTimeMillis);
    } catch (IOException e) {
        LOGGER.error("Error getting creation time for file {}", this.file, e);
        throw new RuntimeIoException(e);
    }

}

From source file:com.spectralogic.ds3client.metadata.PosixMetadataRestore_Test.java

@Test
public void restoreFileTimes_Test() throws Exception {
    final BasicHeader basicHeader[] = new BasicHeader[3];
    final BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
    basicHeader[0] = new BasicHeader(
            MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_CREATION_TIME,
            String.valueOf(attr.creationTime().toMillis()));
    basicHeader[1] = new BasicHeader(
            MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_ACCESS_TIME,
            String.valueOf(attr.lastAccessTime().toMillis()));
    basicHeader[2] = new BasicHeader(
            MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_LAST_MODIFIED_TIME,
            String.valueOf(attr.lastModifiedTime().toMillis()));
    final Metadata metadata = genMetadata(basicHeader);
    final PosixMetadataRestore posixMetadataRestore = new PosixMetadataRestore(metadata, file.getPath(),
            MetaDataUtil.getOS());/*from   w ww .j a  v a 2s.  co m*/
    posixMetadataRestore.restoreFileTimes();
    final BasicFileAttributes fileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
    Assert.assertEquals(String.valueOf(fileAttributes.creationTime().toMillis()),
            String.valueOf(basicHeader[0].getValue()));
    Assert.assertEquals(String.valueOf(fileAttributes.lastModifiedTime().toMillis()),
            String.valueOf(basicHeader[2].getValue()));
}

From source file:com.spectralogic.ds3client.metadata.WindowsMetadataRestore_Test.java

@Test
public void restoreFileTimes_Test() throws Exception {
    final BasicHeader basicHeader[] = new BasicHeader[3];

    final Path filePath = ResourceUtils.loadFileResource(FILE_NAME);

    final BasicFileAttributes attr = Files.readAttributes(filePath, BasicFileAttributes.class);
    basicHeader[0] = new BasicHeader(METADATA_PREFIX + MetadataKeyConstants.KEY_CREATION_TIME,
            String.valueOf(attr.creationTime().toMillis()));
    basicHeader[1] = new BasicHeader(METADATA_PREFIX + MetadataKeyConstants.KEY_ACCESS_TIME,
            String.valueOf(attr.lastAccessTime().toMillis()));
    basicHeader[2] = new BasicHeader(METADATA_PREFIX + MetadataKeyConstants.KEY_LAST_MODIFIED_TIME,
            String.valueOf(attr.lastModifiedTime().toMillis()));
    final Metadata metadata = genMetadata(basicHeader);
    final WindowsMetadataRestore windowsMetadataRestore = new WindowsMetadataRestore(metadata,
            filePath.toString(), MetaDataUtil.getOS());
    windowsMetadataRestore.restoreFileTimes();
    final BasicFileAttributes fileAttributes = Files.readAttributes(filePath, BasicFileAttributes.class);
    Assert.assertEquals(String.valueOf(fileAttributes.creationTime().toMillis()),
            String.valueOf(basicHeader[0].getValue()));
    Assert.assertEquals(String.valueOf(fileAttributes.lastModifiedTime().toMillis()),
            String.valueOf(basicHeader[2].getValue()));
}

From source file:org.cryptomator.webdav.jackrabbit.EncryptedDir.java

@Override
protected void determineProperties() {
    final Path path = ResourcePathUtils.getPhysicalPath(this);
    properties.add(new ResourceType(ResourceType.COLLECTION));
    properties.add(new DefaultDavProperty<Integer>(DavPropertyName.ISCOLLECTION, 1));
    if (Files.exists(path)) {
        try {/*  w ww.jav a  2s.c  om*/
            final BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
            properties.add(new DefaultDavProperty<String>(DavPropertyName.CREATIONDATE,
                    FileTimeUtils.toRfc1123String(attrs.creationTime())));
            properties.add(new DefaultDavProperty<String>(DavPropertyName.GETLASTMODIFIED,
                    FileTimeUtils.toRfc1123String(attrs.lastModifiedTime())));
        } catch (IOException e) {
            LOG.error("Error determining metadata " + path.toString(), e);
            // don't add any further properties
        }
    }
}

From source file:org.sigmah.server.file.impl.BackupArchiveManagerImpl.java

/**
 * Returns the creation date of the given file or the 1st january 1970
 * if an error occured while trying to read the date.
 * // w w w.  ja  va2 s .  c  om
 * @param file File to access.
 * @return Creation date of the given file.
 */
private Date getCreationDate(File file) {
    try {
        final BasicFileAttributeView view = Files.getFileAttributeView(file.toPath(),
                BasicFileAttributeView.class);
        final BasicFileAttributes attributes = view.readAttributes();

        return new Date(attributes.creationTime().toMillis());

    } catch (IOException e) {
        return new Date(0L);
    }
}

From source file:se.trixon.filebydate.Operation.java

private Date getDate(File sourceFile) throws IOException, ImageProcessingException {
    Date date = new Date(System.currentTimeMillis());
    DateSource dateSource = mProfile.getDateSource();

    if (dateSource == DateSource.FILE_CREATED) {
        BasicFileAttributes attr = Files.readAttributes(sourceFile.toPath(), BasicFileAttributes.class);
        date = new Date(attr.creationTime().toMillis());
    } else if (dateSource == DateSource.FILE_MODIFIED) {
        BasicFileAttributes attr = Files.readAttributes(sourceFile.toPath(), BasicFileAttributes.class);
        date = new Date(attr.lastModifiedTime().toMillis());
    } else if (dateSource == DateSource.EXIF_ORIGINAL) {
        Metadata metadata;/*from   w w  w  . j ava2  s.co m*/
        Directory directory = null;

        try {
            metadata = ImageMetadataReader.readMetadata(sourceFile);
            directory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
            date = directory.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
        } catch (NullPointerException | ImageProcessingException ex) {
            String message;
            if (directory == null) {
                message = String.format(Dict.Dialog.ERROR_EXIF_NOT_FOUND.toString(),
                        sourceFile.getAbsolutePath());
            } else {
                message = String.format(Dict.Dialog.ERROR_FILE_FORMAT_NOT_SUPPORTED.toString(),
                        sourceFile.getAbsolutePath());
            }

            throw new ImageProcessingException(message);
        }
    }

    return date;
}

From source file:com.spectralogic.ds3client.metadata.MACMetadataRestore_Test.java

@Test
public void restoreFileTimes_Test() throws Exception {

    if (Platform.isMac()) {
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yy:HH:mm");
        final BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
        final BasicHeader basicHeader[] = new BasicHeader[3];
        basicHeader[0] = new BasicHeader(
                MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_CREATION_TIME,
                String.valueOf(attr.creationTime().toMillis()));
        basicHeader[1] = new BasicHeader(
                MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_ACCESS_TIME,
                String.valueOf(attr.lastAccessTime().toMillis()));
        basicHeader[2] = new BasicHeader(
                MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_LAST_MODIFIED_TIME,
                String.valueOf(attr.lastModifiedTime().toMillis()));
        final Metadata metadata = genMetadata(basicHeader);
        final MACMetadataRestore macMetadataRestore = new MACMetadataRestore(metadata, file.getPath(),
                MetaDataUtil.getOS());/*w  w w. j ava2s  .  com*/
        macMetadataRestore.restoreFileTimes();
        final BasicFileAttributes fileAttributes = Files.readAttributes(file.toPath(),
                BasicFileAttributes.class);
        Assert.assertEquals(simpleDateFormat.format(fileAttributes.creationTime().toMillis()),
                simpleDateFormat.format(Long.valueOf(basicHeader[0].getValue())));
        Assert.assertEquals(simpleDateFormat.format(fileAttributes.lastModifiedTime().toMillis()),
                simpleDateFormat.format(Long.valueOf(basicHeader[2].getValue())));

    }

}

From source file:org.application.backupsync.PathName.java

public JSONObject getAttrs() throws IOException, JSONException {
    JSONObject result;/*w  w w.jav a  2 s .c o  m*/
    BasicFileAttributes attr;
    DosFileAttributes dosAttr;
    PosixFileAttributes posixAttr;

    result = new JSONObject();
    attr = Files.readAttributes(this.path, BasicFileAttributes.class);

    result.append("ctime", attr.creationTime().toMillis());
    result.append("mtime", attr.lastModifiedTime().toMillis());
    //result.append("symlink", attr.isSymbolicLink()); //Redundant
    result.append("size", attr.size());

    if (System.getProperty("os.name").startsWith("Windows")) {
        dosAttr = Files.readAttributes(this.path, DosFileAttributes.class);

        result.append("dos:archive", dosAttr.isArchive());
        result.append("dos:hidden", dosAttr.isHidden());
        result.append("dos:readonly", dosAttr.isReadOnly());
        result.append("dos:system", dosAttr.isSystem());
    } else {
        posixAttr = Files.readAttributes(this.path, PosixFileAttributes.class);

        result.append("posix:symlink", posixAttr.isSymbolicLink());
        result.append("posix:owner", posixAttr.owner());
        result.append("posix:group", posixAttr.group());
        result.append("posix:permission", PosixFilePermissions.toString(posixAttr.permissions()));
    }

    return result;
}

From source file:org.duracloud.retrieval.mgmt.RetrievalWorkerTest.java

@Test
public void testRetrieveToFile() throws Exception {
    RetrievalWorker worker = createRetrievalWorker(true);
    File localFile = new File(tempDir, "retrieve-to-file-test");
    assertFalse(localFile.exists());//from ww  w . j  av  a 2s.  c  o m

    worker.retrieveToFile(localFile, null);
    assertTrue(localFile.exists());

    // Test timestamps
    BasicFileAttributes fileAttributes = Files.readAttributes(localFile.toPath(), BasicFileAttributes.class);
    assertEquals(testTime, fileAttributes.creationTime().toMillis());
    assertEquals(testTime, fileAttributes.lastAccessTime().toMillis());
    assertEquals(testTime, fileAttributes.lastModifiedTime().toMillis());

    // Test file value
    String fileValue = FileUtils.readFileToString(localFile);
    assertEquals(fileValue, contentValue);

    // Test failure
    worker = createBrokenRetrievalWorker(true);
    localFile = new File(tempDir, "retrieve-to-file-failure-test");
    assertFalse(localFile.exists());

    try {
        worker.retrieveToFile(localFile, null);
        fail("Exception expected with non-matching checksum");
    } catch (IOException expected) {
        assertNotNull(expected);
    }
}

From source file:de.tiqsolutions.hdfs.BasicFileAttributeViewImpl.java

Map<String, Object> readAttributes(String attributes) throws IOException {
    BasicFileAttributes attr = readAttributes();
    List<String> attrlist = Arrays.asList(attributes.split(","));
    boolean readall = attrlist.contains("*");
    Map<String, Object> ret = new HashMap<>();
    if (readall || attrlist.contains("fileKey"))
        ret.put("fileKey", attr.fileKey());
    if (readall || attrlist.contains("creationTime"))
        ret.put("creationTime", attr.creationTime());
    if (readall || attrlist.contains("isDirectory"))
        ret.put("isDirectory", attr.isDirectory());
    if (readall || attrlist.contains("isOther"))
        ret.put("isOther", attr.isOther());
    if (readall || attrlist.contains("isRegularFile"))
        ret.put("isRegularFile", attr.isRegularFile());
    if (readall || attrlist.contains("isSymbolicLink"))
        ret.put("isSymbolicLink", attr.isSymbolicLink());
    if (readall || attrlist.contains("lastAccessTime"))
        ret.put("lastAccessTime", attr.lastAccessTime());
    if (readall || attrlist.contains("lastModifiedTime"))
        ret.put("lastModifiedTime", attr.lastModifiedTime());
    if (readall || attrlist.contains("size"))
        ret.put("size", attr.size());
    return ret;/*from   w  w w .j  a  v a 2s  . c om*/
}