Java – how to delete double quotes when reading CSV

public class CSVTeast {
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");
      }


}

Output is

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 .

Expected output

Value [date= POLICY_CHANGE_EFFECTIVE_DATE,name=AGENCY_NAME]

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

Solution

You can try through string The replace () method passes a value

So your code is:

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");
  }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>