java – 如何在读取CSV时删除双引号
发布时间:2020-12-14 06:04:32 所属栏目:Java 来源:网络整理
导读:public class CSVTeast { public static void main(String[] args) { CSVTeast obj = new CSVTeast(); obj.run(); } public void run() { String csvFile = "D:text.csv"; BufferedReader br = null; String line = ""; String cvsSplitBy = "~"; try { br
public class CSVTeast { public static void main(String[] args) { CSVTeast obj = new CSVTeast(); obj.run(); } public void run() { String csvFile = "D:text.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[] csvRead = line.split(cvsSplitBy); System.out.println("Value [date= " + csvRead[5] + ",name=" + csvRead[9]+"]"); } } 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("Done"); } } 输出是 Value [date= "POLICY_CHANGE_EFFECTIVE_DATE",name="AGENCY_NAME"] Value [date= "2014-04-01",name="USI INSURANCE SERVICES]--this value stated with double qoutes but not end with same . 预期产出 Value [date= POLICY_CHANGE_EFFECTIVE_DATE,name=AGENCY_NAME] Value [date= 2014-04-01,name=USI INSURANCE SERVICES] 解决方法
您可以尝试通过String.replace()方法传递值.
所以你的代码是: public class CSVTeast { public static void main(String[] args) { CSVTeast obj = new CSVTeast(); obj.run(); } public void run() { String csvFile = "D:text.csv"; BufferedReader br = null; String line = ""; String cvsSplitBy = "~"; try { br = new BufferedReader(new FileReader(csvFile)); while ((line = br.readLine()) != null) { String[] csvRead = line.split(cvsSplitBy); System.out.println("Value [date= " + csvRead[5].replace(""","") + ",name=" + csvRead[9].replace(""","")+"]"); } } 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("Done"); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |