Program 20.6 File Info

Program 20.6 reads a filename from the command line and returns various information about those files using these methods.

import java.io.File;
import java.io.IOException;


public class FileInfo {


  public static void main(String[] args) {
  
    for (int i = 0; i < args.length; i++) {
      File f = new File(args[i]);
      if (f.exists()) {
        System.out.println("getName: " + f.getName());
        System.out.println("getPath: " + f.getPath());
        System.out.println("getAbsolutePath: " + 
          f.getAbsolutePath());
        System.out.println("getParent: " + f.getParent());
        if (f.canWrite()) System.out.println(f.getName() + 
          " is writable.");
        if (f.canRead()) System.out.println(f.getName() + 
          " is readable.");
        if (f.isFile()) {
           System.out.println(f.getName() + " is a file.");
        }
        else if (f.isDirectory()) {
          System.out.println(f.getName() + " is a directory.");
        }
        else {
          System.out.println("What is this?");
        }
        if (f.isAbsolute()) {
          System.out.println(f.getName() + 
            " is an absolute path.");
        }
        else {
          System.out.println(f.getName() + 
            " is not an absolute path.");
        }
        System.out.println("Last Modified" + 
          f.lastModified());
        System.out.println(f.getName() + " is " + 
          f.length() + " bytes.");
        System.out.println(f.getName() + " is " + 
          f.length() + " bytes.");
      }  
      else {
         System.out.println("I'm sorry. I can't find the file "
           + args[i]);
      }
      
    }
  
  }

}

Copyright 1996-8 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home