Sharing tutorials on creating, deleting, and renaming Java directories and files

Source: Internet
Author: User
The code is as follows: Copy code

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 ();
 }
}

Source code download package jsp/2012/0320/24025.html "> http://down.111cn.net/down/code/jsp/2012/0320/24025.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.