程序分为界面和功能两个类,代码较长,我做了必要注释,请耐心看完,请大神指出运行没反应的原因(尽量不要写新代码给我,在原代码上修正,便于我理解)
实在没积分了,无法悬赏
界面部分
/*
* 界面
* @version 1.0
* 主要功能: 将查找目录内(含子目录)的指定后缀名文件复制到输出目录
*/
package check;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class appearance {
static File OldPath=null; //定义文件对象
static File NewPath=null;
static String LastName=null; //定义后缀名对象
static int x=0; //用于对话框判断
public static void main(String[] args){
Frame f=new Frame("文件查找");
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Label la1=new Label("查找目录:");
Label la2=new Label("输出目录:");
Label la3=new Label("后缀名:");
TextField t1=new TextField(50);
TextField t2=new TextField(50);
TextField t3=new TextField(20);
Button b1=new Button("浏览");
Button b2=new Button("浏览");
Button b3=new Button("确定");
JFileChooser choose1=new JFileChooser();
JFileChooser choose2=new JFileChooser();
choose1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
choose2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
t1.setEditable(false);
t2.setEditable(false);
f.setBounds(200,150,520,200);
p1.setLayout(new FlowLayout());
p2.setLayout(new FlowLayout());
p3.setLayout(new FlowLayout());
f.setLayout(new BorderLayout());
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
f.setVisible(false);
System.exit(0);
}
});
b1.addActionListener(new ActionListener(){ //按钮监听(文件选择器)
public void actionPerformed(ActionEvent arg0) {
choose1.showDialog(new JLabel(),"选择文件夹");
OldPath=choose1.getSelectedFile();
t1.setText(OldPath.getAbsolutePath());
}
});
b2.addActionListener(new ActionListener(){ //按钮监听(文件选择器)
public void actionPerformed(ActionEvent arg){
choose2.showDialog(new JLabel(),"选择文件夹");
NewPath=choose2.getSelectedFile();
t2.setText(NewPath.getAbsolutePath());
}
});
b3.addActionListener(new ActionListener(){ //按钮监听(调用功能模块)
public void actionPerformed(ActionEvent arg1){
LastName=t3.getText();
if(t1.getText().trim().length()<1||t2.getText().trim().length()<1||t3.getText().trim().length()<1){//对话框处理
x=JOptionPane.showConfirmDialog(f,"请输入完整","提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.ERROR_MESSAGE);
if(x==JOptionPane.OK_OPTION)
System.exit(0);
}
Function.cheek(OldPath, NewPath); //调用功能方法,传递文件对象参数
}
});
f.add(p1,"North"); //添加组件
f.add(p2,"Center");
f.add(p3,"South");
p1.add(la1);
p1.add(t1);
p1.add(b1);
p2.add(la2);
p2.add(t2);
p2.add(b2);
p2.add(la3);
p2.add(t3);
p3.add(b3);
f.setVisible(true);
}
}
功能部分
/*
* 功能
* @version 1.0
*/
package check;
import java.io.*;
class Function {
static BufferedInputStream bis=null; //文件输入输出流,用于复制文件
static BufferedOutputStream bos=null;
public static void cheek(File OldPath,File NewPath){ //功能方法 接受界面传值
if(!NewPath.exists()){
NewPath.mkdir();
}
ergodic(OldPath,NewPath); //递归,扫描子目录内所有文件
}
private static void ergodic(File oldPath, File newPath) {
File[] files=oldPath.listFiles();
for(File f:files){
if(f.isDirectory()){
ergodic(f,newPath);
} if(f.getName().endsWith(appearance.LastName)){
copy(f,newPath); //文件复制,
}
}
}
private static void copy(File f, File newPath) { //文件复制
String Name=newPath.getName();
File newfile=new File(f,Name);
try{
bis=new BufferedInputStream(new FileInputStream(f));
bos=new BufferedOutputStream(new FileOutputStream(newfile));
byte[] by=new byte[1024];
int len=0;
while((len=bis.read(by))!=-1){
bos.write(by,0,len);
}
bos.close();
bis.close();
}catch(IOException e){
}
}
}