Java library to read csv and sppecify custom delimiter more than 1 char(string) "|%|"

 

问题

https://stackoverflow.com/questions/68076695/java-library-to-read-csv-and-sppecify-custom-delimiter-more-than-1-charstring

Need to read a csv file with custom string delimiter for example "|%|". Currently I am using CSV commons library, but it only allows splitting by char and not string. I would prefer splitting by sticking to CSV commons library, but open to other libraries as well.

解答

这个问题需要按自定义的分隔符(字符串而非一个字符)读取csv 文件,Java 实现则代码较长。

Java 下的开源包 SPL 很容易写,只要 1 句:


A

1

=file("custom.csv").import@t(;,"|%|")

SPL提供了JDBC 供 JAVA 调用,把上面的脚本存为readcsv.splx,在 JAVA 中以存储过程的方式调用脚本文件:

Class.forName("com.esproc.jdbc.InternalDriver");

con= DriverManager.getConnection("jdbc:esproc:local://");

st=con.prepareCall("call readcsv ()");

st.execute();

或在JAVA 中以 SQL 方式直接执行 SPL 串:

st = con.prepareStatement("==file(\"custom.csv\").import@t (;,\"|%|\")");
st.execute();

SPL 源代码:https://github.com/SPLWare/esProc

问答搜集