Java Library to Read CSV and Specify Custom Delimiter More Than 1 char(string) %
Question
Need to read a CSV file with custom string delimiter like "|%|". 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.
Answer
You need to read a CSV file by the user-defined delimiter (a string instead of a character). It is roundabout to do this in Java.
But it is very simple to get this done using SPL, an open-source Java package. Only one line of code is enough:
A |
|
1 |
=file("custom.csv").import@t(;,"|%|") |
SPL offers JDBC driver to be invoked by Java. Just store the above SPL script as readcsv.splx and invoke it in Java as you call a stored procedure:
…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st=con.prepareCall("call readcsv ()");
st.execute();
…
Or execute the SPL string within a Java program as we execute a SQL statement:
…
st = con.prepareStatement("==file(\"custom.csv\").import@t (;,\"|%|\")");
st.execute();
…
View SPL source code.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL
Chinese version