package com.imti.work.cla;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/** 多个文件的合并。 */
public class FileMerge extends JFrame implements ActionListener
{
private static final long serialVersionUID = -7749716816572931666L;
private JMenuBar bar = new JMenuBar();
private JMenu menu = new JMenu("文件");
private JMenuItem chooseFolder = new JMenuItem("选择文件夹");
private JLabel label = new JLabel("输入最终文件名:");
private JTextField textfield = new JTextField(12);
private JButton button = new JButton("合并");
private JTextArea area = new JTextArea();
//文件扩展名
private JLabel filter = new JLabel("输入文件扩展名(如:.txt):");
private JTextField filterEdit = new JTextField(12);
private String path = null;
//文件夹下所有的以结尾的文件。
private ArrayList
/** 构造方法 */
public FileMerge()
{
this.setTitle("文件合并工具");
this.setBounds(new Rectangle(520, 350));
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
chooseFolder.addActionListener(this);
menu.add(chooseFolder);
bar.add(menu);
this.add(bar);
this.add(label);
this.add(textfield);
button.addActionListener(this);
this.add(button);
this.add(filter);
this.add(filterEdit);
area.setColumns(60);
area.setRows(15);
area.setLineWrap(true);
area.setWrapStyleWord(true);
JScrollPane jsp = new JScrollPane(area);
jsp.setVisible(true);
this.add(jsp);
this.pack();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(chooseFolder))
{
if("".equals(filterEdit.getText()))
{
JOptionPane.showMessageDialog(this, "输入文件扩展名!","提示",JOptionPane.OK_OPTION);
return;
}
//打开文件夹。
JFileChooser filechooser = new JFileChooser();
filechooser.setDialogType(JFileChooser.DIRECTORIES_ONLY);
filechooser.setDialogTitle("选择一个文件夹");
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int res = filechooser.showOpenDialog(this);
if(res == JFileChooser.APPROVE_OPTION)
{
path = filechooser.getSelectedFile().getAbsolutePath();
if(!"".equals(path) && null != path)
{
area.setText("");
fileNames = new ArrayList
area.append("文件夹位置:"+path+"/n");
getFiles(path);
for(int i = 0;i < fileNames.size();i++)
{
area.append(fileNames.get(i)+"/n");
}
area.append("总共包含"+fileNames.size()+"个"+filterEdit.getText()+"文件/n");
}
else
{
JOptionPane.showMessageDialog(this, "请选择一个正确的文件夹!","提示",JOptionPane.OK_OPTION);
return;
}
}
}
else if(e.getSource().equals(button))
{
//执行合并操作。
if(null == path)
{
JOptionPane.showMessageDialog(this, "请选择文件夹!","提示",JOptionPane.OK_OPTION);
return;
}
if("".equals(textfield.getText()))
{
JOptionPane.showMessageDialog(this, "输入最终文件名!","提示",JOptionPane.OK_OPTION);
return;
}
else if("".equals(filterEdit.getText()))
{
JOptionPane.showMessageDialog(this, "输入文件扩展名!","提示",JOptionPane.OK_OPTION);
return;
}
else
{
if(null != path)
{
File file = null;
BufferedReader br = null;
FileOutputStream fos = null;
try
{
//要保存到的位置。
file = new File(path+"//"+textfield.getText());
fos = new FileOutputStream(file,true);
for(int i = 0;i < fileNames.size();i++)
{
String filename= fileNames.get(i);
br = new BufferedReader(new FileReader(filename));
String temp = null;
while((temp = br.readLine()) != null)
{
fos.write(temp.getBytes());
fos.write("/r/n".getBytes());
}
br.close();
fos.flush();
}
fos.close();
} catch (IOException e1)
{
e1.printStackTrace();
}
JOptionPane.showMessageDialog(this, "合并完成!","提示",JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
// 取得文件夹下所有的以.java结尾的文件。
public void getFiles(String path)
{
File file = new File(path);
//如果是文件夹。
if(file.isDirectory())
{
File[] files = file.listFiles();
for(File f :files)
{
//如果是文件,保存文件名。
if(f.isFile() && f.getName().endsWith(filterEdit.getText()))
{
fileNames.add(f.getPath());
}
else
{
getFiles(f.getAbsolutePath());
}
}
}
}
/**
* @param args
*/
public static void main(String[] args)
{
new FileMerge();
}
}