Java Library to Read CSV and Specify Custom Delimiter More Than 1 char(string) %

Question

Source: 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 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.