我有一个 XML 文件,如下所示
<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>light Belgian waffles covered with strawberries and
whipped cream
</description>
<calories>900</calories>
</food>
</CATALOG>
我需要使用java
编程将此文件复制到另一个文件。以下是我复制文件的java代码
try {
File f1 = new File("source.xml");
File f2 = new File("destination.xml");
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
} catch (FileNotFoundException ex) {
System.out
.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
} catch (IOException e7) {
System.out.println(e7.getMessage());
}
这段代码确实复制了文件,但问题在于将源文件的所有内容都复制到了一行中,我需要保持源文件的原始结构。任何人有更好的想法来复制文件并保持其原始结构?谢谢