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,"TestEmail@fakeemail.com","TEST_FNAME","TEST_LNAME"
33333,"TestEmail@fakeemail.com","ACTV","TEST_FNAME","TEST_LNAME"
Expected 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
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());
Of course, you can do this in one line:
Files.write(Paths.get(fileName), new String(Files.readAllBytes(Paths.get(fileName))).replace("\"", "").getBytes());