===== 主程序 =====
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by fang on 16/8/6.
*/
public class Connect {
public static int skipBefore = 5;
public static int skipend = 4;
// 基线,达到这个行数则写到文件
public static int baseLine = 900000;
public static String baseDir = "";
public static String dataDir = "";
public static String outputDir = "";
public static int outputIndex = 1;
public static void main(String[] args) {
List table = new ArrayList<>();
baseDir = args[0];
skipBefore = Integer.parseInt(args[1]);
skipend = Integer.parseInt(args[2]);
baseLine = Integer.parseInt(args[3]);
dataDir = baseDir + "/data";
outputDir = baseDir + "/output";
System.out.println("所有csv文件请放在data文件夹下");
System.out.println("合并时,跳过前"+skipBefore+"行");
System.out.println("合并时,跳过后"+skipend+"行");
System.out.println("合并后的csv文件,放在output文件夹下");
System.out.println("\n\n");
System.out.println("正在合并文件,请等待...");
File file=new File(dataDir);
File[] files = file.listFiles();
for(int i=0;i baseLine){
write(table);
table = new ArrayList<>();
}
List lines = getLines(files[i].getAbsolutePath());
table.addAll(lines.subList(skipBefore, lines.size() - skipend));
}
write(table);
System.out.println("合并文件...成功");
}
public static List getLines(String fileName) {
List table = new ArrayList<>();
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
// 一次读入一行,直到读入null为文件结束
while ((line = reader.readLine()) != null) {
table.add(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return table;
}
public static void write(List table){
try {
FileWriter fw = new FileWriter(outputDir+"/final"+outputIndex+".csv");
for (int i = 0; i < table.size(); i++) {
fw.write(table.get(i)+"\n");
}
fw.close();
System.out.println(outputDir+"/final"+outputIndex+".csv 合并完成");
outputIndex ++;
} catch (IOException e) {
e.printStackTrace();
}
}
}
===== 脚本 =====
windows下运行bat脚本
* 参数1:合并程序的根目录
* 参数2:对每个文件跳过开头5行
* 参数3:对每个文件跳过结尾4行
* 参数4:超过90w条数据,开始合并一次
java -Xmx512m -cp excel-1.0.jar Connect "C://excel_link" 5 4 900000
pause