开发者

How to convert a monolithic CSV String into a List<String>?

I have a CSV stored in a String:

A开发者_StackOverflow社区bc, Def, Ghi, Jkl, Mno[CR|LF]
Abc, Def, Ghi, Jkl, Mno[CR|LF]
Abc, Def, Ghi, Jkl, Mno[CR|LF]
Abc, Def, Ghi, Jkl, Mno[CR|LF]

When I open the file in Notepad++ and use "Show All Characters" I see the newline character at the end of each line represented by the [CR|LF] notation I indicated above.

How do convert this monolithic String into a List<String> where each line above represents a separate String in the List but without the [CR|LF] characters?


I think it should be as simple as:

String allData /* = readCSVFromFile() */;
String[] rows = allData.split("\r\n");
List<String> rowList = Arrays.asList(rows);


Use a BufferedReader. Something like this:

List<String> rows = new ArrayList<String>();
BufferedReader r = new BufferedReader(new FileReader("file.csv"));
String line = null;
while ((line = r.readLine()) != null)
{
    rows.add(line);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜