java – 如何在读取CSV时删除双引号

前端之家收集整理的这篇文章主要介绍了java – 如何在读取CSV时删除双引号前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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");
      }


}@H_301_3@ 
 

输出

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 .@H_301_3@ 
 

预期产出

Value [date= POLICY_CHANGE_EFFECTIVE_DATE,name=AGENCY_NAME]

Value [date= 2014-04-01,name=USI INSURANCE SERVICES]@H_301_3@

解决方法

您可以尝试通过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");
  }
}@H_301_3@
原文链接:https://www.f2er.com/java/239990.html

猜你在找的Java相关文章