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"); } }