分享Java目錄和檔案的建立、刪除和更名教程

來源:互聯網
上載者:User
 代碼如下 複製代碼

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

//目錄和檔案的建立、刪除和更名

public class FileUseDemo extends JFrame{

 JTextField jtfPath;

 public FileUseDemo(){
  super("目錄和檔案的建立、刪除和更名"); //調用父類建構函式

  jtfPath=new JTextField(16); //執行個體化檔案路徑輸入框
  JButton jbFile=new JButton("選擇"); //執行個體化檔案選擇按鈕
  JPanel panel=new JPanel(); //執行個體化面板,用於容納輸入框和按鈕
  panel.add(new JLabel("檔案名稱: ")); //增加組件到面板
  panel.add(jtfPath);
  panel.add(jbFile);
  JPanel panel2=new JPanel();
  panel2.add(new JButton(new CreateFileAction()));  //執行個體化按鈕,並增加到面板2上
  panel2.add(new JButton(new RenameFileAction()));
  panel2.add(new JButton(new DeleteFileAction()));

  jbFile.addActionListener(new ActionListener(){ //選擇檔案按鈕事件處理
   public void actionPerformed(ActionEvent event) {
    JFileChooser fileChooser=new JFileChooser();  //執行個體化檔案選取器
    fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);  //設定檔案選擇模式,此處為檔案和目錄均可
    if (fileChooser.showOpenDialog(FileUseDemo.this)==JFileChooser.APPROVE_OPTION){ //彈出檔案選取器,並判斷是否點擊了開啟按鈕
     String fileName=fileChooser.getSelectedFile().getAbsolutePath(); //得到選擇檔案或目錄的絕對路徑
     jtfPath.setText(fileName);
    }
   }
  });

  Container container=getContentPane(); //得到容器
  container.add(panel,BorderLayout.NORTH); //增加組件到容器
  container.add(panel2,BorderLayout.CENTER);

  setSize(330,120); //設定視窗尺寸
  setVisible(true); //設定視窗可視
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關閉視窗時退出程式
 }

 class CreateFileAction extends AbstractAction { //建立新檔案
  public CreateFileAction() {
      super("建立"); //調用父類建構函式
  }
  public void actionPerformed(ActionEvent e) {
   String filename=jtfPath.getText(); //從輸入框得到檔案名稱
   File sfile=new File(filename); //執行個體化一個檔案
   try{
    if (!sfile.exists()){ //如果檔案不存在
     if (sfile.createNewFile()==true){ //建立檔案成功
      showMessage(filename+" 新檔案建立成功."); //顯示資訊
     }
     else{
      showMessage(filename+" 新檔案建立失敗.");
     }
    }
    else{
     showMessage(filename+" 原檔案已存在.");
    }
   }
   catch (Exception ex){
    ex.printStackTrace(); //列印錯誤資訊
   }
  }
 }

 class RenameFileAction extends AbstractAction { //重新命名檔案
  public RenameFileAction() {
      super("重新命名");
  }
  public void actionPerformed(ActionEvent e) {
   String filename=JOptionPane.showInputDialog("輸入新檔案名稱"); //輸入新檔案名稱對話方塊
   File sfile=new File(jtfPath.getText()); //源檔案
   File f=new File(filename); //新檔案
   sfile.renameTo(f); //重新命名
   showMessage(sfile.getName()+" 重新命名成功."); //顯示資訊
   jtfPath.setText(f.getAbsolutePath()); //更新輸入框的檔案名稱
  }
 }

 class DeleteFileAction extends AbstractAction { //刪除檔案
  public DeleteFileAction() {
      super("刪除");
  }
  public void actionPerformed(ActionEvent e) {
   File sfile=new File(jtfPath.getText()); //源檔案
   try{
    sfile.delete(); //刪除檔案
    showMessage(sfile.getName()+" 刪除成功."); //顯示資訊

   }
   catch(Exception ex){
    showMessage(sfile.getName()+" 刪除失敗.");
    ex.printStackTrace();
   }
  }
 }

 private void showMessage(String message){
  JOptionPane.showMessageDialog(FileUseDemo.this,message); //顯示資訊
 }

 public static void main(String[] args){
  new FileUseDemo();
 }
}

源碼下載包 jsp/2012/0320/24025.html">http://down.111cn.net/down/code/jsp/2012/0320/24025.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.