[英]How to convert a monolithic CSV String into a List<String>?
I have a CSV stored in a String:我有一个 CSV 存储在字符串中:
Abc, 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.当我在 Notepad++ 中打开文件并使用“ Show All Characters
”时,我会在上面指出的[CR|LF]
符号表示的每一行末尾看到换行符。
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?如何将此整体 String 转换为List<String>
,其中上面的每一行代表List
中的一个单独的 String 但没有[CR|LF]
字符?
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
.使用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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.