我试图通过解析xml数据来创建csv文件。当我执行此代码时,每次数据都会附加到文件中,而不是在新文件中写入数据或覆盖旧文件。
File fXmlFile = new File("d:/formdata.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("record");
System.out.println("----------------------------");
List< Map<String, String>> list = new ArrayList<Map<String,String>>();
for (int temp = 0; temp < nList.getLength(); temp++) {
Map<String, String> map = new HashMap<String, String>();
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
map.put("Item No", eElement.getElementsByTagName("item_no").item(0).getTextContent());
map.put("Description", eElement.getElementsByTagName("description").item(0).getTextContent());
map.put("price", eElement.getElementsByTagName("price").item(0).getTextContent());
map.put("base qty", eElement.getElementsByTagName("base_qty").item(0).getTextContent());
map.put("Var qty", eElement.getElementsByTagName("var_qty").item(0).getTextContent());
list.add(map);
}
}
generateCsvFile("E:\\testCSV.csv", list );
generateCsvfile()
private static void generateCsvFile(String sFilename, List< Map<String, String>> test) {
try {
FileWriter writer = new FileWriter(sFilename,true);
for (Map<String, String> map : test) {
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String,String> mapEntry = (Map.Entry<String,String>) iterator.next();
//ystem.out.println("key: " + mapEntry.getKey() + ", value:" + mapEntry.getValue());
writer.append(mapEntry.getValue());
writer.append(",");
iterator.remove();
}
}
writer.flush();
writer.close();
}catch(Exception e){
}
}
如何覆盖文件?
答案 0 :(得分:1)
根据question的答案,FileWriter
构造函数的第二个参数实际上是决定是追加还是覆盖的标志。
尝试从此
更改构造函数语句FileWriter writer = new FileWriter(sFilename,true);
到这个
FileWriter writer = new FileWriter(sFilename);
答案 1 :(得分:1)
您将'true'传递给generateCsvFile()中的FileWriter构造函数。根据Javadoc API,如果append为true,则字节将写入文件的末尾而不是开头http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.io.File,boolean)
答案 2 :(得分:0)
注意可能的语法错误,但概念保持不变。 在filewriter变量声明之后将它放在generateCsvFile中。
if (sFilename.exists()) {
try {
sFilename.delete();
}
catch (IOException e) {
e.printStackTrace();
return;
}
}
基本上,您所做的是检查文件是否存在,如果存在则尝试删除它。但是,如果操作失败,则取消整个方法,从而阻止应用程序将信息附加到现有文件。
答案 3 :(得分:0)
你的generateCsvFile()应该看起来像这样:
private static void generateCsvFile(String sFilename, List< Map<String, String>> test) {
try {
FileWriter writer = new FileWriter(sFilename,true);
for (Map<String, String> map : test) {
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String,String> mapEntry = (Map.Entry<String,String>) iterator.next();
writer.append(mapEntry.getValue());
writer.append(",");
}
writer.append("\n");
}
writer.flush();
writer.close();
} catch(Exception e) {
}
}