1. 讀取系統盤符,為了安全起見,不在選項框中顯示系統盤c及項目盤d
2.可創建文件並追加從JTextArea中添加的內容;
3.可刪除文件及遞歸刪除目錄;
4. 遞歸列出指定目錄下所有的文件及目錄;
5.文件系統搜索功能實現
6. 恰當地使用布局
package com.wt010.file;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* @author konecms
* @date 2018年1月31日 下午5:02:24
*
*/
public class CManager extends JFrame implements ActionListener,FocusListener {
private JButton btnCreate, btnDelete,btnList,btnSearch;
private JTextField txtFile,txtSearch;
private JComboBox cmb;
private JTextArea ta;
public CManager() {
// TODO Auto-generated constructor stub
init();
}
public void init() {
fr();
}
public void fr() {
this.setTitle("文件管理系統");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
BorderLayout bl = new BorderLayout();
FlowLayout fl = new FlowLayout();
GridLayout gl = new GridLayout(2, 1);
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
this.setLayout(bl);
jp1.setLayout(fl);
jp2.setLayout(fl);
getContentPane().add("North", jp1);
getContentPane().add("Center", jp2);
cmb = new JComboBox();
File[] files = File.listRoots();
for (int i = 0; i < files.length; i++) {
if (!files[i].toString().equals("C:\\")
&& !files[i].toString().equals("D:\\"))
cmb.addItem(files[i]);
}
jp1.add(cmb);
txtFile = new JTextField(20);
txtFile.setText("請輸入文件或目錄 ");
txtFile.addFocusListener(this);
jp1.add(txtFile);
txtSearch = new JTextField(10);
txtSearch.setText("請輸入搜索關鍵詞 ");
txtSearch.addFocusListener(this);
jp1.add(txtSearch);
btnCreate = new JButton("創建文件");
btnCreate.addActionListener(this);
btnDelete = new JButton("刪除文件");
btnDelete.addActionListener(this);
btnList = new JButton("目錄文件");
btnList.addActionListener(this);
btnSearch = new JButton("搜索");
btnSearch.addActionListener(this);
jp1.add(btnCreate);
jp1.add(btnDelete);
jp1.add(btnList);
jp1.add(btnSearch);
ta = new JTextArea(15,66);
JScrollPane sp = new JScrollPane(ta);
jp2.add(sp);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
String filename = txtFile.getText();
String root = cmb.getSelectedItem().toString();
if (filename.trim().equals("")) {
JOptionPane.showMessageDialog(this, "請輸入需要文件或目錄名稱 。 ");
return;
}
filename = root + filename;
//點擊搜索按鈕事件
if(source==btnSearch) {
}
//點擊刪除按鈕事件
if (source == btnDelete) {
boolean bl = delete(filename);
if (bl)
JOptionPane.showMessageDialog(this, "刪除成功 ! ");
else
JOptionPane.showMessageDialog(this, "刪除失敗 ! ");
}
//點擊創建文件按鈕事件
if (source == btnCreate) {
File file = new File(filename);
if (file.exists()) {
try {
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file,true), "UTF-8");
BufferedWriter buf = new BufferedWriter(writer);
buf.write("\r\n"+ta.getText());
buf.close();
JOptionPane.showMessageDialog(this, "追加內容成功!");
ta.setText(null);
} catch (UnsupportedEncodingException | FileNotFoundException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, e1.getMessage());
} catch (IOException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, e1.getMessage());
}
return;
}
try {
if (file.createNewFile()) {
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
BufferedWriter buf = new BufferedWriter(writer);
buf.append(ta.getText());
buf.close();
JOptionPane.showMessageDialog(this, "創建成功!");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, e1.getMessage());
}
}
if(source==btnList){
listFile(filename);
}
if(source==btnSearch) {
ta.setText(null);
String w=txtSearch.getText().trim();
if(w.equals("")) return;
listSearch(filename,w);
}
}
@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==txtFile) {
String txt=txtFile.getText().trim();
if(txt.equals("請輸入文件或目錄"))
txtFile.setText("");
}
if(e.getSource()==txtSearch) {
String txt=txtSearch.getText().trim();
if(txt.equals("請輸入搜索關鍵詞"))
txtSearch.setText("");
}
}
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==txtSearch) {
String txt=txtSearch.getText().trim();
if(txt.equals(""))
txtSearch.setText("請輸入搜索關鍵詞");
}
if(e.getSource()==txtFile) {
String txt=txtFile.getText().trim();
if(txt.equals(""))
txtFile.setText("請輸入文件或目錄");
}
}
//點擊搜索事件
public void listSearch(String dirpath,String w) {
File file=new File(dirpath);
File[] files=file.listFiles();
for(int i=0;i<files.length;i++) {
String myfile=files[i].getAbsolutePath();
if(myfile.indexOf(w)!=-1) {
ta.append(myfile+"\r\n");
}
if(files[i].isDirectory()) {
listSearch(files[i].getAbsolutePath(),w);
}
}
}
//點擊目錄文件按鈕事件
public void listFile(String filename){
File file=new File(filename);
if(!file.exists()||!file.isDirectory()){
return;
}
File[] dirList=file.listFiles();
for(int i=0;i<dirList.length;i++){
ta.append(dirList[i].getAbsolutePath()+"\r\n");
if(dirList[i].isDirectory()){
listFile(dirList[i].getAbsolutePath());
}
}
}
public boolean delete(String filename) {
File file = new File(filename);
if (!file.exists()) {
return false;
}
if (file.isDirectory()) {
return deleteDirectory(filename);
}
if (file.isFile()) {
return deleteFile(filename);
}
return false;
}
public boolean deleteDirectory(String dirFile2) {
File file = new File(dirFile2);
File[] dirFile = file.listFiles();
for (int i = 0; i < dirFile.length; i++) {
if (dirFile[i].isFile()) {
deleteFile(dirFile[i].getAbsolutePath());
}
if (dirFile[i].isDirectory()) {
deleteDirectory(dirFile[i].getAbsolutePath());
}
}
if (file.delete()) {
return true;
}
return false;
}
public boolean deleteFile(String filename) {
File file = new File(filename);
if (file.delete())
return true;
else
return false;
}
public static void main(String[] args) {
CManager util = new CManager();
util.setVisible(true);
util.pack();
}
}
效果:

