Home > Software engineering >  How to replace double quotes in headers and rows in a CSV file in Java
How to replace double quotes in headers and rows in a CSV file in Java

Time:06-18

I'm new to Java. I have the following CSV file located in my local folder: C:\Users\MyFile\myFile.csv. The first line of the file is the header. I would like to replace the double quotes from headers and values in the file and replace the file in the same location. I used this code how to remove double quotes while reading CSV to mimic the logic, but couldn't succeed. 

Actual myFile.csv (sample records):

"ID","EMAIL","FIRSTNAME","LASTNAME"             
 99999,"[email protected]","TEST_FNAME","TEST_LNAME"
 33333,"[email protected]","ACTV","TEST_FNAME","TEST_LNAME"

Expected myFile.csv (sample records):

ID,EMAIL,FIRSTNAME,LASTNAME             
99999,[email protected],TEST_FNAME,TEST_LNAME
33333,[email protected],ACTV,TEST_FNAME,TEST_LNAME

CodePudding user response:

Read the contents, remove quotes, write back:

String contents = new String(Files.readAllBytes(Paths.get(fileName)));
contents = contents.replace("\"", "");
Files.write(Paths.get(fileName), contents.getBytes());
  • Related