public class FileDemonstration
1: // Fig. 14.4: FileDemonstration.java
2: // Demonstrating the File class.
3: import java.io.File;
4:
5: public class FileDemonstration
6: {
7: // display information about file user specifies
8: public void analyzePath( String path )
9: {
10: // create File object based on user input
11: File name = new File( path );
12:
13: if ( name.exists() ) // if name exists, output information about it
14: {
15: // display file (or directory) information
16: System.out.printf(
17: "%s%s\n%s\n%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s",
18: name.getName(), " exists",
19: ( name.isFile() ? "is a file" : "is not a file" ),
20: ( name.isDirectory() ? "is a directory" :
21: "is not a directory" ),
22: ( name.isAbsolute() ? "is absolute path" :
23: "is not absolute path" ), "Last modified: ",
24: name.lastModified(), "Length: ", name.length(),
25: "Path: ", name.getPath(), "Absolute path: ",
26: name.getAbsolutePath(), "Parent: ", name.getParent() );
27:
28: if ( name.isDirectory() ) // output directory listing
29: {
30: String directory[] = name.list();
31: System.out.println( "\n\nDirectory contents:\n" );
32:
33: for ( String directoryName : directory )
34: System.out.printf( "%s\n", directoryName );
35: } // end else
36: } // end outer if
37: else // not file or directory, output error message
38: {
39: System.out.printf( "%s %s", path, "does not exist." );
40: } // end else
41: } // end method analyzePath
42: } // end class FileDemonstration
43:
44: /*************************************************************************
45: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
46: * Pearson Education, Inc. All Rights Reserved. *
47: * *
48: * DISCLAIMER: The authors and publisher of this book have used their *
49: * best efforts in preparing the book. These efforts include the *
50: * development, research, and testing of the theories and programs *
51: * to determine their effectiveness. The authors and publisher make *
52: * no warranty of any kind, expressed or implied, with regard to these *
53: * programs or to the documentation contained in these books. The authors *
54: * and publisher shall not be liable in any event for incidental or *
55: * consequential damages in connection with, or arising out of, the *
56: * furnishing, performance, or use of these programs. *
57: *************************************************************************/