how can i read out multiple csv files and combine them into one using java
问题
I am trying to read multiple csv files (for example 1 with first names and gender and 1 with streetnames or whatever) and create a new csv file with all the information merged but i don't know how i have to do that in Java. At the moment i can read out a csv file and i have hard coded a writer to create a new csv file but now i don't know how to continue.
My first csv file looks like this:
anzahl;vorname;geschlecht;position
207;Marie;w;2 ... n records
My second file looks like this:
STRASSE,STRANAM,HSZ
1,Aachener Str.,1 ... n records
Basically if headers don't match, all new header titles(columns) should be added after original header and their values according to that order.
For example the new csv file should look like this:
anzahl;vorname;geschlecht;position;STRASSE;STRANAM;HSZ
207;Marie;w;2;1;Aachener Str.;1
That's my code right now
public void doCsvReading() throws IOException {
var fileName = "D:/IntelliJ/Offis_Project/src/main/resources/Vornamen_2020_Koeln.csv";
try {
var fr = new FileReader(fileName, StandardCharsets.UTF_8);
var reader = new CSVReader(fr);
String[] nextLine;
while((nextLine = reader.readNext()) != null) {
for (var e : nextLine) {
System.out.println(e);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void doCsvWriting() throws IOException {
CSVWriter writer = new CSVWriter(new FileWriter("Odin.csv"));
List<String[]> loki = new ArrayList<>();
String[] gkvHeader = new String[] {"id_intern",
"FirstName",
"LastName",
"FirstBirthName",
"LastBirthName",
"Gender",
"BirthDate",
"BirthYear",
"DeathDate",
"PlaceNow",
"StreetNow",
"HouseNumberNow",
"PostCodeNow",
"CityOfBirth",
"PostCodeBirth"};
loki.add(gkvHeader);
String[] row1 = new String[] {"1", "Bob", "Waters", "Bob", "Waters", "m", "09.06.2021", "2021", "","Oldenburg","Ammerländer Heerstaße","15","26129","Aurich","26721"};
loki.add(row1);
writer.writeAll(loki);
writer.close();
}
解答
这个问题需要依次将两个行数相同的csv 按行号横向拼接成一个 csv,Java 实现则代码较长。
用Java 下的开源包 SPL 很容易写,只要 1 句:
A |
|
1 |
>file("result.csv").export@cw(file("1.csv").import@w(;,";").(~|file("2.csv").import@wc()(#))) |
SPL提供了JDBC 供 JAVA 调用,把上面的脚本存为 combinecsv.splx,在 JAVA 中以存储过程的方式调用脚本文件:
…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st=con.prepareCall("call combinecsv()");
st.execute();
…
或在JAVA 中以 SQL 方式直接执行 SPL 串:
…
st = con.prepareStatement("=>file(\"result.csv\").export@cw(file(\"1.csv\").import@w (;,\";\").(~|file(\"2.csv\").import@w c()(#)))");
st.execute();
…
English version