File employe = new File(“E:five / emplo.xml”); File stud = new File(“E:/one/two/student.xml”); 如何将这两个文件合并到一个文件对象中
答案 0 :(得分:0)
如果要合并两个标准文本文件,则只需使用filewriters和filereaders即可。
我认为这不是某些特定于xml的东西,因为我对它们没有经验。
以下是如何读取文件(没有异常处理):
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr); // the only reason I use this is because I am used to line by line handling
String line;
while((line = br.readLine()) != null)
{
// do something with each line
}
您可以将每个文件读入字符串的arraylist,然后使用:
输出FileWriter fout = new FileWriter(file, toAppend);
fout.write(msg);
fout.close();
答案 1 :(得分:0)
String[] filenames = new String[]{ "emplo.xml", "student.xml"};
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream("merged.xml");
for (String filename : filenames) {
InputStream inputStream = new BufferedInputStream(new FileInputStream(filename);
org.apache.commons.io.IOUtils.copy(inputStream, outputStream);
inputStream.close();
}
outputStream.close();<br/>
或者您也可以使用SAXParser