File Metadata

9 0 0
                                    

metadata - this is a data of a data. If your files contains a data, then metadata is its file attributes.

File Attributes are the following: size, directory, regular file, symbolic link, hidden, modified time, owner of the file, file permission, and file attribute.

Files read attributes to get all the file attributes.

1. readAttributes(Path, String, LinkOption...) - The String here is the attributes to read.

2. readAttributes(Path, Class<A>, LinkOption...) - Class<A> is the type of the attributes.

Interface for different Attributes View

1. BasicFileAttributeView - for basic attributes

2. DosFileAttributeView - extended basic attributes that supports DOS attributes

3. PosixFileAttributeView - extended basic attributes that supports POSIX or Portable operating system for UNIX. It includes file owner, group owner and nine related access permissions.

4. FileOwnerAttributeView - for file owner

5. AclFileAttributeView - for File Access Control list

6. UserDefinedFileAttributeView - for user defined like storing MIME type in solaris.


Just add this on the previous program

=====================

public static void main(String[] args) throws IOException{

        ....

        System.out.println("""

       |.....

       |5. Show Basic Attributes

       |6. Show Dos Attributes

       |7. Show Disc Usage in File Store 

        """);

       .....

       switch(funcNumber){

             ......

             case 5 -> showBasicAttributes(path2);

              case 6 - > showDosAttributes(path2);

              case 7 -> showFileStoreAttributes(path2);

             .........

       }

       ..........

}

private static void showBasicAttributes(Path thePath) throws IOException{

        BasicFileAttributes attr = Files.readAttributes(thePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);

       System.out.println("file size: " + attr.size());

       System.out.println("Is Directory: " + attr.isDirectory());

       System.out.println("Is Regular File: " + attr.isRegularFile());

       System.out.println("Is Symbolic Link: " + attr.isSymbolicLink());

       System.out.println("Is other (not regular or etc..): " + attr.isOther());

        System.out.println("Creation time: " + attr.creationTime());

        System.out.println("Last modified time: " + attr.lastModifiedTime());

        System.out.println("last access: " + attr.lastAccessTime());

}

private static void showDosAttributes(Path thePath) throws IOException, UnsupportedOperationException {

      DosFileAttributes attr = Files.readAttributes(thePath, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);

       System.out.println("Read only: " + attr.readOnly());

        System.out.println("System: " + attr.isSystem());

        System.out.println("Hidden: " + attr.isHidden());

         System.out.println("Archive: " + attr.isArchive());

}

private static void showFileStoreAttributes(Path thePath) throws IOException{

         FileStore fileStore = Files.getFileStore(thePath);

         System.out.println("Total space: " + fileStore.getTotalSpace()/1024);

         System.out.println("Used space: " + (fileStore.getTotalSpace() - fileStore.getUnallocatedSpace())/1024);

         System.out.println("Available space: " + fileStore.getUsableSpace()/1024);

}

=====================


Note: Other things you can do with the files metadata

1. Setting a File or group owner - you can translate a name into an object so you can store as a file owner or a group owner using UserPrincipalLookUpService.

        UserPrincipal owner = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName("Name");

        Files.setOwner(path, owner);

2. User defined filed attributes - to create and track your own attributes. You used UserDefinedFileAttributeView.class

3. PosixFileAttributes - i did not add this since it throws UnsupportedOperationException. You used PosixFileAttributes.class and PosixFilePermissions.


Java ProgrammingWhere stories live. Discover now