首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

采用java程序在源文件下替换内容

2013-03-14 
采用java程序在源文件上替换内容在实际项目中,采用了jaxb生成相应的xml,但是在xml头中出现了standaloney

采用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" 替换成空

热点排行