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