gpt4 book ai didi

java - 追加到现有的 xml 文件

转载 作者:行者123 更新时间:2023-12-02 07:41:56 25 4
gpt4 key购买 nike

我创建了一个方法来在 xml 文件中写入我的信息

Element performances = new Element("performances");
Document doc = new Document(performances);

performances.setAttribute(new Attribute("date", dateFormat.format(cal.getTime())));

//Uptime
Element uptime = new Element("uptime");
uptime.addContent(new Element("days").setText(new Long(duptime).toString()));
uptime.addContent(new Element("hours").setText(new Long(huptime).toString()));
uptime.addContent(new Element("minutes").setText(new Long(muptime).toString()));
uptime.addContent(new Element("seconds").setText(new Long(suptime).toString()));
doc.getRootElement().addContent(uptime);
//TotalHitsCount
doc.getRootElement().addContent(new Element("totalhitcount").setText(new Long(stats.gettotal_hit_count()).toString()));
//EmittedBytes
doc.getRootElement().addContent(new Element("emittedbytes").setText(new Long(stats.getemitted_bytes()).toString()));
//Avghitsec
doc.getRootElement().addContent(new Element("avghitsec").setText(new Float(stats.getavg_hit_sec()).toString()));
//Avgbyteshit
doc.getRootElement().addContent(new Element("avgbyteshit").setText(new Long(stats.getavgbytes_hit()).toString()));
//Avgbps
doc.getRootElement().addContent(new Element("avgbps").setText(new Long(stats.getavgbps()).toString()));
//Total threads
doc.getRootElement().addContent(new Element("totalthreads").setText(new Long(stats.gettotal_threads()).toString()));
//Idle threads
doc.getRootElement().addContent(new Element("idlethreads").setText(new Long(stats.getidle_threads()).toString()));

XMLOutputter xmlOutput = new XMLOutputter();

xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter("/home/mazzy/Scrivania/perfor_"+dateFormat.format(cal.getTime())+".xml"));

我会第一次创建文件,然后在第二次写入时将信息 append 到现有文件上,而不是每次都重新创建新文件

最佳答案

FileWriter类有一个带有 append 参数的构造函数。所以你的代码将如下所示:

xmlOutput.output(doc, new FileWriter("/home/mazzy/Scrivania/perfor_"+dateFormat.format(cal.getTime())+".xml"), true);

关于java - 追加到现有的 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11465160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com