java 替换为空_如何使用Java将CSV的空单元格替换为NULL

我输入了CSV . 其中一列在单元格中没有任何 Value . 在将数据写入另一个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();

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将CSV文件转换为XML,可以使用Java中的以下步骤: 1. 读取CSV文件并将其存储在Java对象中。可以使用Java IO类库中的BufferedReader和StringTokenizer类来实现。 2. 创建XML文档并添加根元素。 3. 遍历CSV数据并为每个记录创建一个XML元素。 4. 将XML元素添加到根元素中。 5. 将XML文档写入文件或将其发送到Web服务。 以下是一个简单的示例代码,它将CSV文件转换为XML: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import org.w3c.dom.Document; import org.w3c.dom.Element; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; public class CsvToXml { public static void main(String[] args) { try { BufferedReader csvReader = new BufferedReader(new FileReader("input.csv")); String row; String[] headers = csvReader.readLine().split(","); // Create XML document and root element DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.newDocument(); Element rootElement = doc.createElement("data"); doc.appendChild(rootElement); while ((row = csvReader.readLine()) != null) { String[] data = row.split(","); // Create XML element for each record Element record = doc.createElement("record"); rootElement.appendChild(record); for (int i = 0; i < data.length; i++) { // Add CSV header as XML element name Element element = doc.createElement(headers[i]); element.appendChild(doc.createTextNode(data[i])); record.appendChild(element); } } csvReader.close(); // Write XML document to file FileWriter fileWriter = new FileWriter("output.xml"); fileWriter.write(doc.toString()); fileWriter.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 注意:这是一个简单的示例代码,实际情况可能会更复杂,需要根据实际需求进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值