package com.testSplitByNumber; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import com.csvreader.CsvReader; public class CombineFile { public static List<Integer> list = new ArrayList<Integer>();/*存放有序的分割文件标号*/ public static List<File> list2 = new ArrayList<File>(); /*存放有序的分割文件*/ public static String name = null; public static String path = "F:/zh"; public static void main(String args[]){ File file = new File(path); if(! file.exists() || !file.isDirectory()){ System.out.println("文件夹错误!"); }else{ long start = System.currentTimeMillis(); File files[] = file.listFiles(); list = fileNum(files); fileOrder(list); fileName(list, list2); BufferedWriter bw = null; CsvReader reader = null; try { bw = new BufferedWriter(new FileWriter(new File("F:/zzh03.csv"))); int num = 0; /*对每一个文件进行处理*/ for(int i = 0;i < list2.size();i++){ File f = list2.get(i); reader = new CsvReader(f.getPath(),',',Charset.forName("GBK")); reader.setSafetySwitch(false); while(reader.readRecord()){ num++; String string[] = reader.getValues(); bw.write(writeLine(string)+" "); if(num % 100000 == 0){ bw.flush(); System.out.println("合并文件" + num + "行"); } } } long end = System.currentTimeMillis(); System.out.println("合并完成,共用时"+(end-start)/1000+"s"); } catch (IOException e1) { e1.printStackTrace(); }finally{ if(reader != null){ reader.close(); reader = null; } try { bw.flush(); } catch (IOException e) { e.printStackTrace(); } } } } /** *对csv中的每一行处理 */ public static String writeLine(String strs[]){ StringBuffer sb = new StringBuffer(""); for(int i = 0;i < strs.length;i++){ sb.append(strs[i]+","); } return sb.substring(0, sb.length() - 1).toString(); } /**截取文件标号并存入list*/ public static List<Integer> fileNum(File files[]){ for(int i = 0;i < files.length;i++){ name = files[i].getName().substring(0, files[i].getName().lastIndexOf(".")-1); String n = files[i].getName().substring(files[i].getName().length()-5, files[i].getName().length()-4); list.add(Integer.parseInt(n)); } return list; } /**对文件标号排序*/ public static List<Integer> fileOrder(List<Integer> list){ for(int i = 0;i < list.size()-1;i++){ for(int j = 1;j < list.size() - i;j++){ int temp = 0; if(list.get(j - 1) .compareTo(list.get(j))>0){ temp = list.get(j - 1); list.set((j - 1),list.get(j )); list.set(j , temp); } } } return list; } /**将list对应标号的对应文件放入list2*/ public static List<File> fileName(List<Integer> list,List<File> list2){ for(int i = 0;i < list.size();i++ ){ File file2 = new File(path+"/"+name + list.get(i)+".csv"); list2.add(file2); } return list2; } }