File class used to obtain file and directory information. - Java File Path IO

Java examples for File Path IO:File Attribute

Description

File class used to obtain file and directory information.

Demo Code

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class Main
{
   public static void main(String[] args) throws IOException
   {/*from w  ww  .java2 s .  c o  m*/
      Path path = Paths.get("c:/");

      if (Files.exists(path)) // if path exists, output info about it
      {
         // display file (or directory) information
        System.out.printf("%n%s exists%n", path.getFileName());
        System.out.printf("%s a directory%n", 
          Files.isDirectory(path) ? "Is" : "Is not");
        System.out.printf("%s an absolute path%n", 
          path.isAbsolute() ? "Is" : "Is not");
        System.out.printf("Last modified: %s%n", 
          Files.getLastModifiedTime(path));
        System.out.printf("Size: %s%n", Files.size(path));
        System.out.printf("Path: %s%n", path);
        System.out.printf("Absolute path: %s%n", path.toAbsolutePath());

         if (Files.isDirectory(path)) // output directory listing
         {
            System.out.printf("%nDirectory contents:%n");
            
            // object for iterating through a directory's contents
            DirectoryStream<Path> directoryStream = 
               Files.newDirectoryStream(path);
   
            for (Path p : directoryStream)
               System.out.println(p);
         } 
      } 
      else // not file or directory, output error message
      {
         System.out.printf("%s does not exist%n", path);
      }   
   }
}

Result


Related Tutorials