// // example program using methods of File class // import java.io.File ; public class TestFile { // takes one command-line argument, pathname // builds a File object for that pathname and shows what // happens when we invoke some of the methods of the // File class // note that the pathname can be a relative path or an // absolute path, a file or a directory, and need not // actually exist public static void main(String[] args) { if (args.length == 0) { System.out.println("Argument required") ; System.exit(-1) ; } File f = new File(args[0]) ; System.out.println(args[0] + " exists? " + f.exists()) ; System.out.println(args[0] + " can be read? " + f.canRead() ) ; System.out.println(args[0] + " can be written? " + f.canRead() ) ; System.out.println(args[0] + " is a file? " + f.isFile() ) ; System.out.println(args[0] + " is a directory? " + f.isDirectory() ) ; System.out.println("Absolute pathname for " + args[0] + " is " + f.getAbsolutePath()) ; System.out.println("Name for " + args[0] + " is " + f.getName()) ; System.out.println("Parent for " + args[0] + " is " + f.getParent()) ; System.out.println("Length of " + args[0] + " is " + f.length()) ; if (f.isDirectory()) { String[] fileList = f.list() ; System.out.println("Contents of directory " + args[0] + ":") ; for (int i = 0 ; i < fileList.length ; i++) System.out.println("\t" + fileList[i]) ; } } }