|
Import java. awt .*;
Import java. awt. event .*;
Import java. io .*;
Import javax. swing .*;
// Create, delete, and rename directories and files
Public class FileUseDemo extends JFrame {
JTextField jtfPath;
Public FileUseDemo (){
Super ("creating, deleting, and renaming directories and files"); // call the parent class constructor
JtfPath = new JTextField (16); // input box of the instantiated file path
JButton jbFile = new JButton ("Select"); // select the instantiated File button
JPanel = new JPanel (); // instantiate the panel to hold the input box and buttons
Panel. add (new JLabel ("File name:"); // add a component to the panel
Panel. add (jtfPath );
Panel. add (jbFile );
JPanel panel2 = new JPanel ();
Panel2.add (new JButton (new CreateFileAction (); // instantiate the button and add it to panel 2.
Panel2.add (new JButton (new RenameFileAction ()));
Panel2.add (new JButton (new DeleteFileAction ()));
JbFile. addActionListener (new ActionListener () {// Select File button event processing
Public void actionreceivmed (ActionEvent event ){
JFileChooser fileChooser = new JFileChooser (); // instantiate a file selector
FileChooser. setFileSelectionMode (JFileChooser. FILES_AND_DIRECTORIES); // sets the file selection mode. Both files and directories are supported.
If (fileChooser. showOpenDialog (FileUseDemo. this) = JFileChooser. APPROVE_OPTION) {// The File Selector is displayed, and whether the open button is clicked.
String fileName = fileChooser. getSelectedFile (). getAbsolutePath (); // Obtain the absolute path of the selected file or directory.
JtfPath. setText (fileName );
}
}
});
Container container = getContentPane (); // Obtain the Container
Container. add (panel, BorderLayout. NORTH); // add component to container
Container. add (panel2, BorderLayout. CENTER );
SetSize (330,120); // Set the window size
SetVisible (true); // sets the window visibility.
Setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // exit the program when the window is closed.
}
Class CreateFileAction extends actaction {// create a new file
Public CreateFileAction (){
Super ("create"); // call the parent class constructor
}
Public void actionreceivmed (ActionEvent e ){
String filename = jtfPath. getText (); // get the file name from the input box
File sfile = new File (filename); // instantiate a File
Try {
If (! Sfile. exists () {// if the file does not exist
If (sfile. createNewFile () = true) {// file created successfully
ShowMessage (filename + "new file created successfully."); // Display Information
}
Else {
ShowMessage (filename + "new file creation failed .");
}
}
Else {
ShowMessage (filename + "The original file already exists .");
}
}
Catch (Exception ex ){
Ex. printStackTrace (); // Print the error message
}
}
}
Class RenameFileAction extends AbstractAction {// rename the file
Public RenameFileAction (){
Super ("rename ");
}
Public void actionreceivmed (ActionEvent e ){
String filename = JOptionPane. showInputDialog ("enter new file name"); // enter new file name dialog box
File sfile = new File (jtfPath. getText (); // source File
File f = new File (filename); // new File
Sfile. renameTo (f); // rename
ShowMessage (sfile. getName () + "renamed successfully."); // Display Information
JtfPath. setText (f. getAbsolutePath (); // update the object name in the input box.
}
}
Class DeleteFileAction extends actaction {// delete an object
Public DeleteFileAction (){
Super ("delete ");
}
Public void actionreceivmed (ActionEvent e ){
File sfile = new File (jtfPath. getText (); // source File
Try {
Sfile. delete (); // delete an object
ShowMessage (sfile. getName () + "deleted successfully."); // Display Information
}
Catch (Exception ex ){
ShowMessage (sfile. getName () + "failed to delete .");
Ex. printStackTrace ();
}
}
}
Private void showMessage (String message ){
JOptionPane. showMessageDialog (FileUseDemo. this, message); // Display Information
}
Public static void main (String [] args ){
New FileUseDemo ();
}
} |