I have a CSV that I generate by building StringBuilder and writing to using PrintWriter. Then I read that CSV again and append something to it, but it messes up the cell which has double quotes in it, used to denote inches.
It prints double-quotes twice as 15"
One of the values being added to StringBuilder is this:
String title = "Poly Nuclear 15\" Laptop Series Notebook Intel Windows10+ 7.6V Battery 8GB Memory"
Text t1 = new Text();
t1.setContent(title);
if (title.contains("\"")) {
t1.setContent("Poly Nuclear 15\\\" Laptop Series Notebook Intel Windows10+ 7.6V Battery 8GB Memory");
}
My first output (after writing comma separated String created using StringBuilder) using PrintWriter is this:
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filename, true), StandardCharsets.UTF_8);
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
printWriter.println(stringBuilder.toString());
key,date,ms_id,title,alertId
190-2,2022-02-20 12:35:09,107193,Poly Nuclear 15" Laptop Series Notebook Intel Windows10+ 7.6V Battery 8GB Memory,
Now I am adding the last column's value alertId
at the of each row. I am reading and appending to each row, then writing back to CSV as follows:
// Here below method is called as writeBack("1222") with fixed value.
public void writeBack(String value) {
String filePath = "/dir1/dir2/test.csv";
String key = "alertId"; // column name for which value needs to be added.
InputStreamReader inputStreamReader = new InputStreamReader(new
FileInputStream(filePath), StandardCharsets.UTF_8);
CSVReader reader = new CSVReader(inputStreamReader);
String[] header = reader.readNext();
int columnNum = Arrays.asList(header).indexOf(key);
List<String[]> feedData = reader.readAll();
try {
for (String[] row : feedData) {
row[columnNum] = value;
}
reader.close();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8);
CSVWriter writer = new CSVWriter(outputStreamWriter);
writer.writeNext(header);
writer.writeAll(feedData);
writer.flush();
writer.close();
} catch (Exception e) {
writeLog("ERROR", e);
}
}
My final output is this where everything is correct except the String value has double quotes as 15""
"key","date","ms_id","title","alertId"
"190-2","2022-02-20 12:35:09","107193","Poly Nuclear 15"" Laptop Series Notebook Intel Windows10+ 7.6V Battery 8GB Memory","1222"
What can I do to avoid double quotes in the final output within the cell that denotes inches?
Expected output
"key","date","ms_id","title","alertId"
"190-2","2022-02-20 12:35:09","107193","Poly Nuclear 15\" Laptop Series Notebook Intel Windows10+ 7.6V Battery 8GB Memory","1222"
Thank you @Mark Rotteveel for the pointer tip. This helped me to look for a different separator and also escape more characters.
Realized that CSVReader and CSVWriter has different escape characters too.
I finally solved using the follwing:
import org.apache.commons.lang.StringEscapeUtils;
...
StringEscapeUtils.escapeCsv("Poly Nuclear 15\" Laptop Series, Notebook \\ Intel Windows10+ 7.6V Battery 8GB Memory")
And using this while writing:
import com.opencsv.ICSVWriter;
...
char escapeChar = '\\';
CSVWriter writer = new CSVWriter(outputStreamWriter, ICSVWriter.DEFAULT_SEPARATOR, ICSVWriter.DEFAULT_QUOTE_CHARACTER, escapeChar, ICSVWriter.DEFAULT_LINE_END);