我有以下代码可以从 android 中的 csv 文件读取。当我的列值中有逗号时,它工作得很好。
File file = new File(IMPORT_ITEM_FILE.getPath());
BufferedReader bufRdr = null;
try {
bufRdr = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
}
String line = null;
try {
while ((line = bufRdr.readLine()) != null) {
String[] insertValues = line.split(",");
string column1 = insertValues[1];
string column2 = insertValues[2];
}
bufRdr.close();
}
我的 csv 行类似于:用户名,“一些字符串,带逗号”,“2740740, 2740608”,,“一些字符串,带逗号”
如何转义列值中的逗号以读取列。
最佳答案
我建议您使用http://opencsv.sourceforge.net/ 。如果您的 CSV 文件源文件格式正确,它将为您处理一切。
使用示例:
InputStream csvStream = getAssets().open(yourCSVFile);
InputStreamReader csvStreamReader = new InputStreamReader(csvStream);
CSVReader csvReader = new CSVReader(csvStreamReader);
String[] line;
while ((line = csvReader.readNext()) != null) {
String example = line[0]; // first item of your csv row
}
关于java - 在java中从csv中读取带逗号的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22189879/