Reputation: 65
I'm looking for a Java library which will enable me to easily extract data from a specific column (or multiple columns) of CSV text held in memory e.g. in a String.
For example, extract each 'town' value in the records below into an ArrayList<String>
object or String[]
. Or even further, extract all 'town' and 'country' values from every record.
... I preferably need a solution that doesn't rely on the columns being in fixed positions.
ADDRESS|HOUSE_NO|STREET|TOWN|CITY|COUNTY|COUNTRY
DATA|51|Hill Road|Reading|Berkshire|United Kingdom
DATA|78|Crescent Road|Wallingford|Oxfordshire|United Kingdom
DATA|5|Bonny Crescent|Swindon|Whiltshire|United Kingdom
... note that the entire CSV section in the above example is contained in memory in a single String.
I have already been looking at the option of using an in-memory database engine like H2, but it can't seem to handle running SQL queries against CSV text that's held in memory e.g. in a String. Any suggestions? Thanks in advance
Upvotes: 0
Views: 188
Reputation: 14969
I use opencsv in all my projects. Its a pretty good library, and is available in Maven central so if you are using maven its really easy to add to your project.
Upvotes: 2
Reputation: 82579
Use String.split
String[] tokens = myString.split("\\|");
But I'd like to point out that if you have a database, you shouldn't be storing it as a CSV. You should make a table and have columns for that. So I would strongly consider doing this before you put it in the database, and putting it in the proper columns at that time.
If the ENTIRE file is a single string, I'd do this:
final String delim = "\\|";
Scanner sc = new Scanner(csvFile);
String headersRaw = sc.nextLine();
String[] headers = headersRaw.split(delim);
// process headers if necessary
while(sc.hasNextLine()) {
String[] tokens = sc.nextLine().split(delim);
// process tokens here
}
Upvotes: 0