package Lesson14;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class CombineTool extends JFrame implements ActionListener {
Label lM, lM2, L;
JTextField jt1, jt2;
JButton jb1, jb2, jb3, jb4;
InputStream in;
OutputStream out;
File dir;
JFileChooser fc = new JFileChooser();
JFileChooser fcCom = new JFileChooser();
String stringfile = "";
public CombineTool() {
super("文件合并器");
this.setBounds(300, 400, 400, 300);
this.setLayout(new GridLayout(3, 2, 0, 10));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Init();
Container cc = getContentPane();
cc.setLayout(new FlowLayout());
cc.add(lM);
cc.add(jt1);
cc.add(jb1);
cc.add(lM2);
cc.add(jt2);
cc.add(jb4);
cc.add(jb2);
cc.add(jb3);
cc.add(L);
this.setVisible(true);
}
private void Init() {
// TODO Auto-generated method stub
lM = new Label("文件目录:");
jt1 = new JTextField(20);
jb1 = new JButton("......");
lM2 = new Label("合并文件");
jt2 = new JTextField(20);
jb4 = new JButton("......");
jb2 = new JButton("合并");
jb3 = new JButton("退出");
L = new Label();
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == jb1) {
open();
} else if (source == jb4) {
chooseDir();
} else if (source == jb2) {
try {
Com();
} catch (Exception e2) {
System.out.println(e.toString());
}
} else {
System.exit(0);
}
}
private void Com() throws IOException {
String FilDir = jb1.getText();
String ComDir = jt2.getText();
File fTar = new File(FilDir);
File[] ComList = fTar.listFiles();
System.out.print(ComList[0]);
out = new FileOutputStream(ComDir);
for (int i = 0; i < ComList.length; i++)
{
in = new FileInputStream(ComList[i]);
byte[] a = new byte[in.available()];
in.read(a);
out.write(a);
}
in.close();
out.close();
L.setText("ok");
}
private void chooseDir() {
// TODO Auto-generated method stub
//fcCom.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fcCom.showOpenDialog(this);
dir = fcCom.getSelectedFile();
stringfile = dir.getAbsolutePath();
jt2.setText(new String(stringfile));
}
private void open() {
// TODO Auto-generated method stub
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.showOpenDialog(this);
dir = fc.getSelectedFile();
stringfile = dir.getAbsolutePath();
jt1.setText(new String(stringfile));
}
public static void main(String[] args) {
new CombineTool();
}
}