Example usage for java.nio.file.attribute BasicFileAttributeView readAttributes

List of usage examples for java.nio.file.attribute BasicFileAttributeView readAttributes

Introduction

In this page you can find the example usage for java.nio.file.attribute BasicFileAttributeView readAttributes.

Prototype

BasicFileAttributes readAttributes() throws IOException;

Source Link

Document

Reads the basic file attributes as a bulk operation.

Usage

From source file:Main.java

public static void main(String[] args) {

    Path path = Paths.get("C:/tutorial/Java/JavaFX", "Topic.txt");

    try {/* w  w  w.jav  a  2s.  c  om*/
        BasicFileAttributeView bv = Files.getFileAttributeView(path, BasicFileAttributeView.class);
        BasicFileAttributes ba = bv.readAttributes();
        System.out.println(ba.creationTime());
    } catch (IOException e) {
        System.err.println(e);
    }

}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class);

    BasicFileAttributes attributes = view.readAttributes();
    FileTime lastModifedTime = attributes.lastModifiedTime();
    FileTime createTime = attributes.creationTime();

    long currentTime = Calendar.getInstance().getTimeInMillis();
    FileTime lastAccessTime = FileTime.fromMillis(currentTime);

    view.setTimes(lastModifedTime, lastAccessTime, createTime);
    System.out.println(attributes.lastAccessTime());
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = FileSystems.getDefault().getPath("/home/docs/users.txt");
    // BasicFileAttributes attributes = Files.readAttributes(path,
    // BasicFileAttributes.class);
    BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class);
    BasicFileAttributes attributes = view.readAttributes();

    System.out.println("Creation Time: " + attributes.creationTime());
    System.out.println("Last Accessed Time: " + attributes.lastAccessTime());
    System.out.println("Last Modified Time: " + attributes.lastModifiedTime());
    System.out.println("File Key: " + attributes.fileKey());
    System.out.println("Directory: " + attributes.isDirectory());
    System.out.println("Other Type of File: " + attributes.isOther());
    System.out.println("Regular File: " + attributes.isRegularFile());
    System.out.println("Symbolic File: " + attributes.isSymbolicLink());
    System.out.println("Size: " + attributes.size());
}

From source file:Main.java

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

    try {/*  w ww.  j  a  v a  2 s .  c  o  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: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.
 * //from w  w w  .jav a 2s.com
 * @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:org.sigmah.server.file.impl.BackupArchiveManagerImpl.java

/**
 * Builds the given {@code file} corresponding {@link BackupDTO}.
 * /*  w ww .j a  va  2 s  .  com*/
 * @param file
 *          The backup file path.
 * @return The given {@code file} corresponding {@link BackupDTO}.
 * @throws IOException
 *           If an I/O error occurs.
 * @throws IllegalArgumentException
 *           If the given {@code file} does not reference a valid backup file.
 */
private BackupDTO fromFile(final Path file) throws IOException {

    final String filename = file.getFileName().toString();
    final Matcher matcher = BACKUP_ARCHIVE_NAME_PATTERN.matcher(filename);

    if (!matcher.matches()) {
        throw new IllegalArgumentException("Invalid backup archive file name '" + filename + "'.");
    }

    final Integer organizationId = Integer.parseInt(matcher.group(1));
    final Integer orgUnitId = Integer.parseInt(matcher.group(2));
    final LoadingScope loadingScope = LoadingScope.valueOf(matcher.group(3));
    final String extension = matcher.group(4);

    final BasicFileAttributeView view = Files.getFileAttributeView(file, BasicFileAttributeView.class);
    final BasicFileAttributes attributes = view.readAttributes();

    final BackupDTO result = new BackupDTO();

    result.setOrganizationId(organizationId);
    result.setOrgUnitId(orgUnitId);
    result.setOrgUnitName(orgUnitDAO.findById(orgUnitId).getFullName());
    result.setLoadingScope(loadingScope);
    result.setCreationDate(new Date(attributes.creationTime().toMillis()));
    result.setArchiveFileName(filename);
    result.setRunning(extension.equals(BACKUP_ARCHIVE_TEMP_EXT));

    return result;
}