6

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.

1
  • Character set conversions aside, shouldn't something like new ByteArrayInputStream(string.getBytes()) basically do the job here? Commented Apr 13, 2018 at 16:35

1 Answer 1

15

You can use java.io.StringReader. Example:

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

try (CSVReader reader = new CSVReader(new StringReader(inputString))) {
    // Your code here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.