Java DosFileAttributes get file attribute

Description

Java DosFileAttributes get file attribute

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributes;

public class Main {

  public static void main(String[] args) throws IOException {
    Path pWin = Paths.get("C:\\tmp\\file1.txt");
    DosFileAttributes fa = Files.readAttributes(pWin, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);

    System.out.println("Creation time " + fa.creationTime());
    System.out.println("File key " + fa.fileKey());
    System.out.println("Is directory? " + fa.isDirectory());
    System.out.println("Is other? " + fa.isOther());
    System.out.println("Is regular file? " + fa.isRegularFile());
    System.out.println("Is symbolic link? " + fa.isSymbolicLink());
    System.out.println("Last access time " + fa.lastAccessTime());
    System.out.println("Last modified time " + fa.lastModifiedTime());
    System.out.println("Size " + fa.size());
  }/*www. j a  v  a  2 s. co  m*/
}
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.DosFileAttributes;

public class Main {

   public static void main(String[] args) {
      Path path = FileSystems.getDefault().getPath("/home/docs/users.txt");
      try {//w  ww  .  j  a  v  a2  s.c o m
         DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class);
         DosFileAttributes attributes = view.readAttributes();

         System.out.println("isArchive: " + attributes.isArchive());
         System.out.println("isHidden: " + attributes.isHidden());
         System.out.println("isReadOnly: " + attributes.isReadOnly());
         System.out.println("isSystem: " + attributes.isSystem());

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}



PreviousNext

Related