Finally I found the simple solution how to create CSV file and store the headers and values in separated columns. The example we need to have is:
ColumnA ColumnB
Value1 Value2
I didn’t use any library, using Java’s FileWriter I do the following. Change the comma delimiter from (,) to (;) as in example below.
private static final String COMMA_DELIMITER = ";";
Then define columns name and the rest example code.
private static final String FILE_HEADER = " **ColumnA; ColumnB**";
FileWriter fileWriter = null;
fileWriter = new FileWriter("your_csv_file.csv");
// Write the CSV file header
fileWriter.append(FILE_HEADER.toString());
// Add a new line separator after the header
fileWriter.append(NEW_LINE_SEPARATOR);
fileWriter.append(“Value1”);
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(“Value2”);
fileWriter.append(NEW_LINE_SEPARATOR);
fileWriter.flush();
fileWriter.close();
Please note that you have to write the code correctly with try-catch and finally blocks.
CSVstand forComma Seperated valueand you want aCSVwithout a comma seperator?seperator.