packagemergesql;importjava.awt.BorderLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.nio.ByteBuffer;importjava.nio.channels.FileChannel;importjava.text.SimpleDateFormat;importjava.util.Arrays;importjava.util.Collections;importjava.util.Comparator;importjava.util.Date;importjava.util.List;importjava.util.Scanner;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JPanel;importjavax.swing.JTextField;/*** 合并sql脚本文件工具类
*
*@authorwebapp
**/
public class MergeSqlFileUtil extends JFrame implementsActionListener {/****/
private static final long serialVersionUID = 1L;private final static String fileName = ".sql";/*** 获取当前路径下的所有文件中
*
* @time 2014年8月25日
* @auther yuxu.feng
*@paramfilepath
*@return
*/
private static ListgetFiles(String filepath) {
File file= newFile(filepath);if (!file.exists() | file.listFiles(new MyFileFileter()) == null)return null;
List fileList =Arrays
.asList(file.listFiles(newMyFileFileter()));//最好在这里把 文件过滤掉 只获取文件夹
returnsortFolder(fileList);
}/*** 按照文件夹按照文件名进行升序 或按着文件名的名字进行升序
*
* @time 2014年8月25日
* @auther yuxu.feng
*@paramfiles
*@return
*/
private static List sortFolder(Listfiles) {if (files.size() == 0)return null;
Collections.sort(files,new Comparator() {
@Overridepublic intcompare(File o1, File o2) {if (o1.isDirectory() &&o2.isFile())return -1;if (o1.isFile() &&o2.isDirectory())return 1;if (o1.isDirectory() &&o2.isDirectory())returnsortFolderName(o1.getName(), o2.getName());else
return 0;
}
});returnfiles;
}/*** 根据文件夹名称排序
*
* @time 2014年8月25日
* @auther yuxu.feng
*@paramstartName
*@paramendName
*@return
*/
private static intsortFolderName(String startName, String endName) {if ((parseFloderName(startName) - parseFloderName(endName)) >= 0)return 1;else
return -1;
}/*** 将文件夹的名字(格式为 A.B.C.D) 转换为long型的数字
*
* @time 2014年8月25日
* @auther yuxu.feng
*@paramfloderName
*@return
*/
private static longparseFloderName(String floderName) {
Scanner sc= new Scanner(floderName).useDelimiter("\\.");return (sc.nextLong() << 24) + (sc.nextLong() << 16)+ (sc.nextLong() << 8) +(sc.nextLong());
}/*** 使用NIO进行文件合并
*
* @time 2014年8月25日
* @auther yuxu.feng
*@paramfilepath*/
public static voidmergerNIO(String filepath) {
SimpleDateFormat fd= new SimpleDateFormat("yyyyMMddHHmmss");
String nowTime= fd.format(newDate());
List fileList =getFiles(filepath);if (fileList == null)return;
File fout= new File(filepath + File.separator + nowTime +fileName);try{
@SuppressWarnings("resource")
FileChannel mFileChannel= newFileOutputStream(fout).getChannel();
FileChannel inFileChannel;
List files = null;for(File folder : fileList) {if(folder.isDirectory()) {
files=Arrays.asList(folder.listFiles());//sortFolder(files);
for(File fin : files) {//if (fin.getName().endsWith(fileName)) {//inFileChannel = new FileInputStream(fin)//.getChannel();//inFileChannel.transferTo(0, inFileChannel.size(),//mFileChannel);//
//inFileChannel.close();//}
if(fin.getName().endsWith(fileName)) {
inFileChannel= newFileInputStream(fin)
.getChannel();
inFileChannel.transferTo(0, inFileChannel.size(),
mFileChannel);
inFileChannel.close();
mFileChannel.write(ByteBuffer.wrap("\r\n".getBytes()));
}
}
}
}
mFileChannel.close();
}catch(IOException e) {
e.printStackTrace();
}
}/*** 使用普通IO进行拷贝文件
*
* @time 2014年8月27日
* @auther yuxu.feng
*@paramfilepath*/
public static voidmergerIO(String filepath) {
SimpleDateFormat fd= new SimpleDateFormat("yyyyMMddHHmmss");
String nowTime= fd.format(newDate());
List fileList =getFiles(filepath);if (fileList == null)return;
File fout= new File(filepath + File.separator + nowTime +fileName);
BufferedInputStream inBuff= null;
BufferedOutputStream outBuff= null;try{
outBuff= new BufferedOutputStream(newFileOutputStream(fout));
List files = null;
FileInputStream finput= null;for(File folder : fileList) {if(folder.isDirectory()) {
files=Arrays.asList(folder.listFiles());for(File fin : files) {
finput= newFileInputStream(fin);
inBuff= newBufferedInputStream(finput);byte[] b = new byte[finput.available()];intlen;while ((len = inBuff.read(b)) != -1) {
outBuff.write(b,0, len);
}
inBuff.close();
outBuff.flush();
}
}
}
outBuff.close();
}catch(IOException e) {
e.printStackTrace();
}
}/***
* @time 2014年8月27日
* @auther yuxu.feng
*@paramfilepath*/
public static voidmergeByBuffWriter(String filepath) {
SimpleDateFormat fd= new SimpleDateFormat("yyyyMMddHHmmss");
String nowTime= fd.format(newDate());
List fileList =getFiles(filepath);if (fileList == null)return;try{
File fout= new File(filepath + File.separator + nowTime + fileName);//输出文件位置
List files = null;
BufferedReader br= null;
BufferedWriter bw= null;
FileOutputStream fops= newFileOutputStream(fout);
bw= new BufferedWriter(newOutputStreamWriter(
fops,"UTF-8"));//bw.write('\ufeff'); 带有bom格式的utf-8//fops.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF}); 带有bom格式的utf-8
for(File folder : fileList) {if(folder.isDirectory()) {
files=Arrays.asList(folder.listFiles());for(File fin : files) {
FileInputStream finput= newFileInputStream(fin);
br= new BufferedReader(newInputStreamReader(finput,"UTF-8"));char[] cbuf = new char[finput.available()];int len =cbuf.length;int off = 0;int ret = 0;while ((ret = br.read(cbuf, off, len)) > 0) {
off+=ret;
len-=ret;
}
br.close();
bw.write(cbuf,0, off);
bw.newLine();
bw.flush();
}
}
}
bw.close();
}catch(IOException e) {
e.printStackTrace();
}
}// /swing界面 privateJLabel lblUsername;privateJTextField tfUsername;privateJButton btnOK;privateJButton btnExit;publicMergeSqlFileUtil() {
JPanel p1= newJPanel();
p1.setLayout(newBorderLayout());
lblUsername= new JLabel("合并的文件目录:");
tfUsername= new JTextField(20);
p1.add(lblUsername, BorderLayout.WEST);
p1.add(tfUsername, BorderLayout.EAST);
JPanel p2= newJPanel();
p2.setLayout(newBorderLayout());
JPanel p3= newJPanel();
btnOK= new JButton("确定");
btnOK.addActionListener(this);
btnExit= new JButton("取消");
btnExit.addActionListener(this);
p3.add(btnOK);
p3.add(btnExit);this.add(p1, BorderLayout.NORTH);this.add(p2, BorderLayout.CENTER);this.add(p3, BorderLayout.SOUTH);this.setLocation(400, 300);this.setSize(350, 110);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);
}
@Overridepublic voidactionPerformed(ActionEvent e) {if (e.getActionCommand().equals("确定")) {
String path=tfUsername.getText();
Long endIo=System.currentTimeMillis();
MergeSqlFileUtil.mergerIO(path);//MergeSqlFileUtil.mergeByBuffWriter(path);
Long endNio =System.currentTimeMillis();
JOptionPane.showMessageDialog(this, "运行时间为:" + (endNio -endIo));
}else if (e.getActionCommand().equals("取消")) {
System.exit(0);
}
}public static voidmain(String[] args) {newMergeSqlFileUtil();
}
}