删除文件
帮我个忙,输入delete和文件名就可以删除文件.
谢谢!!!
import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; public class DeleteFile extends JFrame{ private JTextField cmdField; private Container container; public DeleteFile(){ super("删除文件"); Container container = getContentPane(); cmdField = new JTextField(20); JButton delete = new JButton("删除"); delete.addActionListener(new DeleteHandler()); container.setLayout(new FlowLayout()); container.add(cmdField); container.add(delete); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getRootPane().setDefaultButton(delete); pack(); setResizable(false); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize(); setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.width)/2); setVisible(true); } public static void main(String[] args) { new DeleteFile(); } private class DeleteHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ String command = cmdField.getText().trim(); if(command.indexOf("delete")==-1){ JOptionPane.showMessageDialog(container, "您输入的命令有误"); } command = command.substring(6).trim(); File file = new File(command); if(!file.isFile()){ JOptionPane.showMessageDialog(container, "您输入的命令中不含文件名或无此文件"); return; } file.delete(); JOptionPane.showMessageDialog(container, "删除成功"); } } }