我输入了 CSV。其中一列在单元格中没有值。将数据写入另一个 CSV 时,我想在该空单元格中写入 NULL。 我尝试用 NULL 替换“”,但随后它在所有单元格中打印 NULL。 另外,我想排除在代码的硬编码列中添加 NULL 文本,即 UniqueId、IsDelete、Rootpid。我希望它们是空的。
这是迄今为止我的代码:
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();
}
}
请您参考如下方法:
不可能,csv中的空字符串和空字符串没有区别。