2012-07-13 56 views
0

我创造了一个方法xml文件的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")); 

,而不是重新创建一个新的文件,每次,我会创造了第一次文件,并在第二次写入追加关于现有文件的信息

+0

你的意思是追加一个全新的DOM后,现有的?如果是这样的话:http://stackoverflow.com/questions/2738448/java-io-how-to-append-to-an-already-existing-text-file?rq=1 – Thilo 2012-07-13 06:26:21

+0

是的,我会追加一个新的DOM后现有的... – Mazzy 2012-07-13 06:31:09

回答

2

FileWriter类具有append参数的构造函数。 所以你的代码看起来像这样:

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