我有以下csv文件,这是逗号分隔,现在从这个文件我需要读取列der_id的值,我已经完成了,但请告诉我如何在java中以更优化的方式实现相同通过扫描仪或通过java中的任何方法,比这更优化..
wert,der_tran,der_id,der_version,cvns_num,cvs_type
AB42126325,0,694698683,0,651626843,13002
AB42126326,0,694698686,0,651626846,13001
目前我正在以这种格式阅读..
public class Parsingcsv {
public static void main(String[] args)
{
Parsingcsv obj = new Parsingcsv();
obj.run();
}
public void run()
{
String csvFile = "C:\\abc_2.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try
{
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] id = line.split(cvsSplitBy);
System.out.println("[der_id= " + id[2] + "]");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("#####################3");
}
}
答案 0 :(得分:0)
分割一行来提取id是低效的,试试这个
String id = line.replaceAll(".*?,.*?,(.*?),.*", "$1");
答案 1 :(得分:0)
你应该只使用一个库。 this question
中有建议