public class FileDemonstration extends JFrame
1: // Fig. 14.37: FileDemonstration.java
2: // Demonstrating the File class.
3: import java.awt.BorderLayout;
4: import java.awt.event.ActionEvent;
5: import java.awt.event.ActionListener;
6: import java.io.File;
7: import javax.swing.JFileChooser;
8: import javax.swing.JFrame;
9: import javax.swing.JOptionPane;
10: import javax.swing.JScrollPane;
11: import javax.swing.JTextArea;
12: import javax.swing.JTextField;
13:
14: public class FileDemonstration extends JFrame
15: {
16: private JTextArea outputArea; // used for output
17: private JScrollPane scrollPane; // used to provide scrolling to output
18:
19: // set up GUI
20: public FileDemonstration()
21: {
22: super( "Testing class File" );
23:
24: outputArea = new JTextArea();
25:
26: // add outputArea to scrollPane
27: scrollPane = new JScrollPane( outputArea );
28:
29: add( scrollPane, BorderLayout.CENTER ); // add scrollPane to GUI
30:
31: setSize( 400, 400 ); // set GUI size
32: setVisible( true ); // display GUI
33:
34: analyzePath(); // create and analyze File object
35: } // end FileDemonstration constructor
36:
37: // allow user to specify file name
38: private File getFile()
39: {
40: // display file dialog, so user can choose file to open
41: JFileChooser fileChooser = new JFileChooser();
42: fileChooser.setFileSelectionMode(
43: JFileChooser.FILES_AND_DIRECTORIES );
44:
45: int result = fileChooser.showOpenDialog( this );
46:
47: // if user clicked Cancel button on dialog, return
48: if ( result == JFileChooser.CANCEL_OPTION )
49: System.exit( 1 );
50:
51: File fileName = fileChooser.getSelectedFile(); // get selected file
52:
53: // display error if invalid
54: if ( ( fileName == null ) || ( fileName.getName().equals( "" ) ) )
55: {
56: JOptionPane.showMessageDialog( this, "Invalid File Name",
57: "Invalid File Name", JOptionPane.ERROR_MESSAGE );
58: System.exit( 1 );
59: } // end if
60:
61: return fileName;
62: } // end method getFile
63:
64: // display information about file user specifies
65: public void analyzePath()
66: {
67: // create File object based on user input
68: File name = getFile();
69:
70: if ( name.exists() ) // if name exists, output information about it
71: {
72: // display file (or directory) information
73: outputArea.setText( String.format(
74: "%s%s\n%s\n%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s",
75: name.getName(), " exists",
76: ( name.isFile() ? "is a file" : "is not a file" ),
77: ( name.isDirectory() ? "is a directory" :
78: "is not a directory" ),
79: ( name.isAbsolute() ? "is absolute path" :
80: "is not absolute path" ), "Last modified: ",
81: name.lastModified(), "Length: ", name.length(),
82: "Path: ", name.getPath(), "Absolute path: ",
83: name.getAbsolutePath(), "Parent: ", name.getParent() ) );
84:
85: if ( name.isDirectory() ) // output directory listing
86: {
87: String directory[] = name.list();
88: outputArea.append( "\n\nDirectory contents:\n" );
89:
90: for ( String directoryName : directory )
91: outputArea.append( directoryName + "\n" );
92: } // end else
93: } // end outer if
94: else // not file or directory, output error message
95: {
96: JOptionPane.showMessageDialog( this, name +
97: " does not exist.", "ERROR", JOptionPane.ERROR_MESSAGE );
98: } // end else
99: } // end method analyzePath
100: } // end class FileDemonstration
101:
102: /*************************************************************************
103: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
104: * Pearson Education, Inc. All Rights Reserved. *
105: * *
106: * DISCLAIMER: The authors and publisher of this book have used their *
107: * best efforts in preparing the book. These efforts include the *
108: * development, research, and testing of the theories and programs *
109: * to determine their effectiveness. The authors and publisher make *
110: * no warranty of any kind, expressed or implied, with regard to these *
111: * programs or to the documentation contained in these books. The authors *
112: * and publisher shall not be liable in any event for incidental or *
113: * consequential damages in connection with, or arising out of, the *
114: * furnishing, performance, or use of these programs. *
115: *************************************************************************/