Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use readAttributes method?

Tags:

java

I don't know how to use readAttributes method of Files class, to access all the file attributes at once. My problem is that I want to print all the attributes in bulk, without calling methods.

I don't want to call methods one by one, as shown below:

Path file = ...;
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);

System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());

System.out.println("isDirectory: " + attr.isDirectory());
System.out.println("isOther: " + attr.isOther());
System.out.println("isRegularFile: " + attr.isRegularFile());
System.out.println("isSymbolicLink: " + attr.isSymbolicLink());
System.out.println("size: " + attr.size());

I would like to access all the BasicFileAttribute at once. If we cannot access all attributes with readAttributes at once,then is there any other way to do this.

like image 708
Fawaz Ahmed Avatar asked Jan 14 '23 05:01

Fawaz Ahmed


1 Answers

This is another example of how to use readAttributes() method of the class BasicFileAttributes class:

   Path path = Paths.get("C:\\Users\\jorgesys\\workspaceJava\\myfile.txt");
    BasicFileAttributes attr;
    try {
     attr = Files.readAttributes(path, BasicFileAttributes.class);
     System.out.println("Creation time: " + attr.creationTime());
     System.out.println("Last access time: " + attr.lastAccessTime());
     System.out.println("Last modified time: " + attr.lastModifiedTime());
    } catch (IOException e) {
     System.out.println("oops un error! " + e.getMessage());
    }
like image 191
Jorgesys Avatar answered Jan 21 '23 15:01

Jorgesys