简体   繁体   中英

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 eg one line of CSV's.

You can use java.io.StringReader . Example:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM