TKPhillyBurb
TKPhillyBurb

Reputation: 171

Use CSVReader from opencsv.CSVReader for a String

I would like to use the CSVReader in opencsv to read a string of comma separated values. I have used this reader with a multipart file in the past wherein the following would be done: CSVReader reader = new CSVReader(new InputStreamReader(fileName.getInputStream());

This is fine for a multipart file however I cannot find a solution for this if I simply want to pass in a string e.g. one line of CSV's.

Upvotes: 6

Views: 5866

Answers (1)

Eien
Eien

Reputation: 1167

You can use java.io.StringReader. Example:

final String inputString = "value1,value2,value3";

try (CSVReader reader = new CSVReader(new StringReader(inputString))) {
    // Your code here
}

Upvotes: 15

Related Questions