-2

文件雇员 = new File("E:five/emplo.xml"); File stud = new File("E:/one/two/student.xml"); 如何将这两个文件合并到一个文件对象中

4

2 回答 2

0

如果你想合并两个标准文本文件,那么你可以只使用文件写入器和文件读取器。

我假设这不是一些特定于 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
}

您可以将每个文件读入字符串数组列表,然后使用以下命令输出:

FileWriter fout = new FileWriter(file, toAppend);
fout.write(msg);
fout.close();
于 2012-11-12T04:42:04.470 回答
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

于 2012-11-12T04:43:14.050 回答