从 csv 返回行
Returning rows from a csv
您好,我有一个如下所示的 csv:
r1c1|r1c2|r1c3
r2c1|r2c2|r2c3
如您所见,它是由字符“|”分隔的
在我的应用程序中,我试图使用输入流来分解它。这是我的代码:
String line = "";
String cvsSplitBy = "|";
try {
File initialFile = new File(myfile.txt);
InputStream targetStream = new FileInputStream(initialFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(targetStream));
while ((line = reader.readLine()) != null) {
String[] RowData = line.split(cvsSplitBy);
String c0 = RowData[0];
String c1 = RowData[1];
String c2 = RowData[2];
Toast.makeText(mainactivity.this, c2, Toast.LENGTH_LONG).show();
}
}catch (IOException ex) {
// handle exception
}
不幸的是,这似乎 return csv 中的每个字符都是一行。上面的 toast 例子 returns 1 then 2.
有人知道如何 return 正确的列吗?
split()
围绕给定 正则表达式 的匹配项拆分字符串,因此使用特殊字符(竖线是其中之一)需要转义以去除其 "powers".
String cvsSplitBy = "\|"
查看文档:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
您好,我有一个如下所示的 csv:
r1c1|r1c2|r1c3
r2c1|r2c2|r2c3
如您所见,它是由字符“|”分隔的
在我的应用程序中,我试图使用输入流来分解它。这是我的代码:
String line = "";
String cvsSplitBy = "|";
try {
File initialFile = new File(myfile.txt);
InputStream targetStream = new FileInputStream(initialFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(targetStream));
while ((line = reader.readLine()) != null) {
String[] RowData = line.split(cvsSplitBy);
String c0 = RowData[0];
String c1 = RowData[1];
String c2 = RowData[2];
Toast.makeText(mainactivity.this, c2, Toast.LENGTH_LONG).show();
}
}catch (IOException ex) {
// handle exception
}
不幸的是,这似乎 return csv 中的每个字符都是一行。上面的 toast 例子 returns 1 then 2.
有人知道如何 return 正确的列吗?
split()
围绕给定 正则表达式 的匹配项拆分字符串,因此使用特殊字符(竖线是其中之一)需要转义以去除其 "powers".
String cvsSplitBy = "\|"
查看文档:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html