I have data in the below format
PAL : PAL : NF : "INCOME"."Taxable"
PAL : PAL : NF : "EXPENSES"."TotalExpenses"
PAL : PAL : NF : "EXPENSES"."Exceptional"
In java, i just want to delimit the data without doing any formatting, in the outputs also quotes should come. I usually use Univocity, when using the below code,
//Simple CSV File Read
List<String[]> allRows;
try {
CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setLineSeparator("\n");
settings.getFormat().setDelimiter(':');
CsvParser parser = new CsvParser(settings);
allRows = parser.parseAll(new FileReader(new File(csvFile)));
int i =0, cols=0;
for(String[] str:allRows){
i++;
cols = str.length;
for(String s:str)
System.out.print(s+" == ");
System.out.println("");
if(i == 10) break;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Output was like below, period symbol is within quotes, i am expecting output to be like input, quotes should come.
PAL == PAL == NF == INCOME"."Taxable
PAL == PAL == NF == EXPENSES"."TotalExpenses
PAL == PAL == NF == EXPENSES"."Exceptional
Expected Output
PAL == PAL == NF == "INCOME"."Taxable"
PAL == PAL == NF == "EXPENSES"."TotalExpenses"
PAL == PAL == NF == "EXPENSES"."Exceptional"
This looks like a combination of a bug in your code and a relaxation of the CSV spec in Univocity.
The input was
Unfortunately, this is NOT valid CSV, as you have a string containing embedded quotes. The correct CSV encoding would have been
The Univocity library seems to have been non-strict about this and guessed that the input was meant to be a single string (since there was no input delimiter) in there. So, after parsing the internal value of that field was
This is the actual contents of the string, without the outside quotes that are required to make it a string literal in Java.
Then when you wrote it out you neglected to add back the surrounding quotes, resulting in the output you see.
Summary:
To fix your problem you have to put back the surrounding quotes yourself
This also fixes the other bug of the extra trailing
==delimiter.