次のような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());
}
このコードはファイルをコピーしますが、問題は、ソースファイルのすべてのコンテンツを1行でコピーすることであり、ソースファイルの元の構造を維持する必要があります。ファイルをコピーして元の構造を維持するためのより良いアイデアを持っている人はいますか?ありがとうございました