0

私は自分の情報を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"));

毎回新しいファイルを再作成する代わりに、最初にファイルを作成し、2 回目の書き込みで既存のファイルに情報を追加します。

4

1 に答える 1

2

FileWriterクラスには、append パラメーターを持つコンストラクターがありますしたがって、コードは次のようになります。

xmlOutput.output(doc, new FileWriter("/home/mazzy/Scrivania/perfor_"+dateFormat.format(cal.getTime())+".xml"), true);
于 2012-07-13T06:33:14.507 に答える