Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to replace double quotes in headers and rows in a CSV file in Java

I am 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,"TestEmail@fakeemail.com","TEST_FNAME","TEST_LNAME"
 33333,"TestEmail@fakeemail.com","ACTV","TEST_FNAME","TEST_LNAME"

Expected myFile.csv (sample records):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ID,EMAIL,FIRSTNAME,LASTNAME             
99999,TestEmail@fakeemail.com,TEST_FNAME,TEST_LNAME
33333,TestEmail@fakeemail.com,ACTV,TEST_FNAME,TEST_LNAME

>Solution :

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());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading