public class FileOrganizer extends JFrame implements ActionListener
1:
2: import javax.swing.*;
3: import java.awt.*;
4: import java.awt.event.*;
5: import java.io.*;
6:
7: public class FileOrganizer extends JFrame implements ActionListener
8: {
9: public static final int WIDTH = 400;
10: public static final int HEIGHT = 300;
11: public static final int NUMBER_OF_CHAR = 30;
12:
13: private JTextField fileNameField;
14: private JTextField firstLineField;
15:
16: public static void main(String[] args)
17: {
18: FileOrganizer gui = new FileOrganizer();
19: gui.setVisible(true);
20: }
21:
22: public FileOrganizer( )
23: {
24: setSize(WIDTH, HEIGHT);
25: WindowDestroyer listener = new WindowDestroyer( );
26: addWindowListener(listener);
27: Container contentPane = getContentPane( );
28: contentPane.setLayout(new FlowLayout( ));
29:
30: JButton showButton = new JButton("Show first line");
31: showButton.addActionListener(this);
32: contentPane.add(showButton);
33:
34: JButton removeButton = new JButton("Remove file");
35: removeButton.addActionListener(this);
36: contentPane.add(removeButton);
37:
38: JButton resetButton = new JButton("Reset");
39: resetButton.addActionListener(this);
40: contentPane.add(resetButton);
41:
42: fileNameField = new JTextField(NUMBER_OF_CHAR);
43: contentPane.add(fileNameField);
44: fileNameField.setText("Enter file name here.");
45:
46: firstLineField = new JTextField(NUMBER_OF_CHAR);
47: contentPane.add(firstLineField);
48: }
49:
50: public void actionPerformed(ActionEvent e)
51: {
52: String actionCommand = e.getActionCommand( );
53:
54: if (actionCommand.equals("Show first line"))
55: showFirstLine();
56: else if (actionCommand.equals("Remove file"))
57: removeFile();
58: else if (actionCommand.equals("Reset"))
59: {
60: fileNameField.setText("Enter file name here.");
61: firstLineField.setText("");
62: }
63: else
64: firstLineField.setText("Unexpected error.");
65: }
66:
67: public void showFirstLine()
68: {
69: BufferedReader fileInput;
70: String firstLine;
71: String fileName = fileNameField.getText();
72: File fileObject = new File(fileName);
73:
74: if ( ! fileObject.exists( ))
75: firstLineField.setText("No such file");
76: else if ( ! fileObject.canRead( ))
77: firstLineField.setText("That file is not readable.");
78: else
79: {
80: try
81: {
82: fileInput =
83: new BufferedReader(new FileReader(fileObject));
84: firstLine = fileInput.readLine( );
85: firstLineField.setText(firstLine);
86: fileInput.close( );
87: }
88: catch(IOException e)
89: {
90: firstLineField.setText("Problem reading from file.");
91: }
92: }
93: }
94:
95: public void removeFile()
96: {
97: BufferedReader fileInput;
98: String firstLine;
99: String fileName = fileNameField.getText();
100: File fileObject = new File(fileName);
101:
102: if ( ! fileObject.exists( ))
103: firstLineField.setText("No such file");
104: else if ( ! fileObject.canWrite( ))
105: firstLineField.setText("Permission denied.");
106: else
107: {
108: if (fileObject.delete())
109: firstLineField.setText("File deleted.");
110: else
111: firstLineField.setText("Could not delete file.");
112: }
113: }
114: }