I need to read a CSV file, replace the column names with double quotes, and replace the file in the same location. Is there any way I can do that in Java?
My original sample columns were: col1
, col2, col3
My desired columns: "col1", "col2", "col3"
Actually, to replace the double quotes in my CSV files, I have been using the following code, but to inject the double quotes, I couldn't figure out the solution.
Path myActualCsvFile = Path.of(fileName);
Files.write(myActualCsvFile, new String(Files.readAllBytes(myActualCsvFile)).replace("\"","").getBytes());
CodePudding user response:
Looks like you are doing this manually rather than using a CSV reader, so your best bet is to simply read the file into a List then simply split the first line by the command ,
like a CSV and then check for the quotation and add them if needed. Finally, merge it back together and write it to file.
The complete code would be something like the following:
Path filePath = Path.of("csvFileTest.csv");
//Read the file
List<String> lines = Files.readAllLines(filePath);
//Print the header for debugging
System.out.println(lines.get(0));
//Get the first line with the column labels and split the string with the comma ,
String[] columnHeaders = lines.get(0).split(",");
//Holding value for the updated line
String updatedLine = "";
//Loop the column labels
for (int i = 0; i < columnHeaders.length; i ){
//Add a " to the start if it doesn't exist already
if (!columnHeaders[i].startsWith("\""))
updatedLine = "\"";
//Add the label
updatedLine = columnHeaders[i];
//Add a " to the end if it doesn't exist already
if (!columnHeaders[i].endsWith("\""))
updatedLine = "\"";
//Add the , to separate the values
updatedLine = ",";
}
//Put the updated header back into the lines array
lines.set(0, updatedLine);
//Print the result for debugging
System.out.println(lines.get(0));
//Write back to the file
Files.write(filePath, lines);
And the output is as follows, we can see that it correctly added the quotes on either side:
col1,col2,col3
"col1","col2","col3",
If you want this to work for all lines in the file then just wrap the middle section inside a loop as well and do the same for each line:
...
//Loop the lines
for (int l = 0; l < lines.size(); l ){
//Get the first line with the column labels and split the string with the comma ,
String[] line = lines.get(0).split(",");
//Holding value for the updated line
String updatedLine = "";
//Loop the column labels
for (int i = 0; i < line.length; i ){
//Add a " to the start if it doesn't exist already
if (!columnHeaders[i].startsWith("\""))
updatedLine = "\"";
//Add the label
updatedLine = columnHeaders[i];
//Add a " to the end if it doesn't exist already
if (!columnHeaders[i].endsWith("\""))
updatedLine = "\"";
//Add the , to separate the values
updatedLine = ",";
}
//Put the updated header back into the lines array
lines.set(l, updatedLine);
}
...
Note: If you are using another method to read the file then don't forget to close the reader before you attempt to write back to the file.