I have input CSV. One of the columns has no value in the cell. While writing the data to another CSV I want to write NULL in that empty cell. I tried to replace "" with NULL but then it prints NULL in all cells. Also, I want to exclude adding NULL text in hard-coded columns of the code i.e. UniqueId, IsDelete, Rootpid. I want them to be empty.
Here is my code so far:
public static void main(String[] args) throws FileNotFoundException, IOException
{
String sourceFilePath = "C://MyData/Emp_Input.csv";
String newFilePath = "C://MyData/Emp_Output.csv";
File sourceCsvFile = new File(sourceFilePath);
File finalCsvFile = new File(newFilePath);
final Charset Encoding = Charset.forName("UTF-8");
String cvsSplitBy = ",";
String line = "";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(finalCsvFile), Encoding));
try (BufferedReader br = new BufferedReader(new FileReader(sourceCsvFile)))
{
line = br.readLine();
String newFileLine = "UniqueID" + cvsSplitBy +line + cvsSplitBy + "IsDelete" + cvsSplitBy + "Rootpid";
writer.write(newFileLine);
writer.newLine();
while ((line = br.readLine()) != null)
{
/* if (line.contains("") )
{
line = line.replace("", "NULL"); // missing code to replace empty cell of a csv with text as NULL
} */
else if (line.contains("TRUE") || line.contains("FALSE"))
{
line = line.replace("TRUE", "1");
line = line.replace("FALSE", "0");
}
newFileLine = cvsSplitBy + line + cvsSplitBy + cvsSplitBy;
writer.write(newFileLine);
writer.newLine();
}
writer.close();
}
}

