前:
idFile#x_y.csv
后:
idFile.csv
例如:
100#1_2.csv + 100#2_2.csv > 100.csv
100#1_2.csv 包含:
"one","two","three"
"a","b","c"
"d","e","f"
100#2_2.csv 包含:
"one","two","three"
"g","h","i"
"j","k","l"
100.csv 包含:
"one","two","three"
"a","b","c"
"d","e","f"
"g","h","i"
"j","k","l"
资源:
//MergeDemo.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
//import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;
public class MergeDemo {
public static void main(String[] args) {
String idFile = "100";
int numFiles = 3;
try {
mergeCsvFiles(idFile, numFiles);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void mergeCsvFiles(String idFile, int numFiles) throws IOException {
// Variables
ArrayList<File> files = new ArrayList<File>();
Iterator<File> iterFiles;
File fileOutput;
BufferedWriter fileWriter;
BufferedReader fileReader;
String csvFile;
String csvFinal = "C:\\out\\" + idFile + ".csv";
String[] headers = null;
String header = null;
// Files: Input
for (int i = 1; i <= numFiles; i++) {
csvFile = "C:\\in\\" + idFile + "#" + i + "_" + numFiles + ".csv";
files.add(new File(csvFile));
}
// Files: Output
fileOutput = new File(csvFinal);
if (fileOutput.exists()) {
fileOutput.delete();
}
try {
fileOutput.createNewFile();
// log
// System.out.println("Output: " + fileOutput);
} catch (IOException e) {
// log
}
iterFiles = files.iterator();
fileWriter = new BufferedWriter(new FileWriter(csvFinal, true));
// Headers
Scanner scanner = new Scanner(files.get(0));
if (scanner.hasNextLine())
header = scanner.nextLine();
// if (scanner.hasNextLine()) headers = scanner.nextLine().split(";");
scanner.close();
/*
* System.out.println(header); for(String s: headers){
* fileWriter.write(s); System.out.println(s); }
*/
fileWriter.write(header);
fileWriter.newLine();
while (iterFiles.hasNext()) {
String line;// = null;
String[] firstLine;// = null;
File nextFile = iterFiles.next();
fileReader = new BufferedReader(new FileReader(nextFile));
if ((line = fileReader.readLine()) != null)
firstLine = line.split(";");
while ((line = fileReader.readLine()) != null) {
fileWriter.write(line);
fileWriter.newLine();
}
fileReader.close();
}
fileWriter.close();
}
}