我已经编写了将一个文件内容复制到另一个文件的代码。但我无法将第二个文件内容复制到第三个文件。 为此,我编写了以下代码:
try {
File infile = new File("d:\\vijay.txt");
File outfile = new File("d:\\ajay.txt");
FileInputStream instream = new FileInputStream(infile);
FileOutputStream outstream = new FileOutputStream(outfile);
byte[] buffer = new byte[1024];
int length;
while ((length = instream.read(buffer)) > 0) {
outstream.write(buffer, 0, length);
}
instream.close();
outstream.close();
System.out.println("File Copied successfully");
} catch (IOException ioe) {
ioe.printStackTrace();
}
请帮助我,在此先感谢。
最佳答案
如果您使用的是 Java 7,我建议您使用 Files
实用程序。例如:
Path source1 = Paths.get("src1.txt");
Path source2 = Paths.get("src2.txt");
Path destination = Paths.get("dest.txt");
out = Files.newOutputStream(destination, CREATE, APPEND);
Files.copy(source1, destination, StandardCopyOption.REPLACE_EXISTING);
Files.copy(source2, destination);
关于java - 我想将2个文件的内容复制到第三个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35500094/