文件操作:创建与删除
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.io.File;
import java.io.IOException;
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 FileCreateAndDeleteUtil extends JFrame implements ActionListener{
private JButton btnCreate,btnDelete;
private JTextField txtFile;
private JComboBox cmb;
public FileCreateAndDeleteUtil() {
// 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);
jp1.add(txtFile);
btnCreate=new JButton("创建文件");btnCreate.addActionListener(this);
btnDelete=new JButton("删除文件");btnDelete.addActionListener(this);
jp1.add(btnCreate);jp1.add(btnDelete);
JTextArea ta=new JTextArea(15,40);
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.equals("")) {
JOptionPane.showMessageDialog(this, "请输入需要文件或目录名称 。 ");
return;
}
filename=root+filename;
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()) {
JOptionPane.showMessageDialog(this, "文件已经存在 。");
return;
}
try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
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) {
FileCreateAndDeleteUtil util=new FileCreateAndDeleteUtil();
util.setVisible(true);
util.pack();
}
}