Reputation: 93
I have a csv file which contains 5 fields with 1 field having embedded newlines. I can read the csv file perfectly using CSVReader [OpenCSV]. I am also able to get individual fields in spite of the embedded newlines. But I want to write another csv file which contains all the fields in the same way but want to ignore ONLY "embedded newlines" and not the traditional end of row newlines. Can someone tell me how can I achieve this?
I am using the code below, but somehow I am still not able to replace "\n" with "". The output of System.out.println(tempLine[0]); still contains embedded newline.
CSVReader reader = new CSVReader(new FileReader(INPUT_FILE), ',');
CSVWriter writer = new CSVWriter(new FileWriter(OUTPUT_FILE), ',');
String [] nextLine;
String [] tempLine = new String[1];
while ((nextLine = reader.readNext()) != null)
{
System.out.println("Tweet: " + nextLine[3] + "\nSentiment: " + nextLine[4]);
tempLine[0] = nextLine[3].replace("\\n", "");
System.out.println(tempLine[0]);
writer.writeNext(tempLine);
}
Thank you for your help!
Upvotes: 4
Views: 13577
Reputation: 626
Use a util method like below. Slightly modified @Jim Garrison's ans. Changed "\\n" to "\n"
private static String[] cleanNewLine(String[] fields) {
String[] newFields = new String[fields.length];
int i = 0;
for (String field : fields) {
if(field != null)
newFields[i] = field.replace("\n", "");
i++;
}
return newFields;
}
Upvotes: 1
Reputation: 86744
After reading in a line, examine each field and remove any newlines you find.
String[] newFields = new String[fields.length];
i=0;
for (String field : fields)
{
newFields[i++] = field.replace("\\n","");
}
Then write the newFields back out using OpenCSV.
Upvotes: 2