我有一個 xml 檔案和字串資料,我想在 xml 檔案中添加字串,我是 Java 初學者,在 xml 語法開始之前混淆了如何打開 xml 和附加字串。下面是xml檔案
<?xml version="1.0"?>
-<AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.02">
-<Fr>
-<FIId>
-<Fin>
<BIC>AAA</BIC>
</Fin>
</FIId>
</Fr>
</AppHdr>
String value="Hello Append with xml";
最終輸出
Hello Append with xml<?xml version="1.0"?>
-<AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.02">
-<Fr>
-<FIId>
-<Fin>
<BIC>AAA</BIC>
</Fin>
</FIId>
</Fr>
</AppHdr>
uj5u.com熱心網友回復:
像這樣嘗試
try {
File mFile = new File("C:\\name.xml");//path to file with name of file
FileInputStream fis = new FileInputStream(mFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringBuilder result = new StringBuilder("Hello Append with xml");
String line = "";
while( (line = br.readLine()) != null){
result.append(line);
result.append(System.getProperty("line.separator"));
}
mFile.delete();
FileOutputStream fos = new FileOutputStream(mFile);
fos.write(result.toString().getBytes());
fos.flush();
} catch (IOException e) {
//exception handling
}
UPD:添加了這一行 result.append(System.getProperty("line.separator"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/382501.html
上一篇:if(!file)和if(file==NULL)的區別
下一篇:如何使用c中的檔案更新用戶資訊
