采用java程序在源文件上替换内容
在实际项目中,采用了jaxb生成相应的xml,但是在xml头中出现了standalone="yes"字样,但实际项目中是不需要它的,要把它去掉。
c:/xmlTest.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><webSites> <webSite open="true" href="www.baidu.com"> <title id="1">百度</title> </webSite> <webSite open="false" href="www.sian.com"> <title id="2">新浪</title> </webSite> <webSite open="false" href="www.163.com"> <title id="3_1">网易</title> <title id="3_2">网易</title> <title id="3_3">网易</title> <title id="3_4">网易</title> </webSite></webSites>
?
采用在源文件的基础上直接修改保存,具体代码如下:
Test.java
public static void formatFile(File file){ try{ BufferedInputStream bin = new BufferedInputStream(new FileInputStream(file)); byte[] buff = new byte[(int)file.length()]; bin.read(buff); FileOutputStream fout = new FileOutputStream(file); String str = new String(buff); String[] lines = str.split("\n"); for(String line : lines){ String line_changed = line.replace("standalone="yes"", ""); fout.write((line_changed+"\n").getBytes()); } fout.flush(); fout.close(); bin.close(); }catch(FileNotFoundException ex){ ex.printStackTrace(); }catch(IOException ioe){ ioe.printStackTrace(); } }public static void main(String[] args) {Test.formatFile(new File("c:/xmlTest.xml"));}?
把?standalone="yes" 替换成空