如何修改 annale2013q3 的方法以将其转换为允许从 2 个文件 f1 和 f2 生成第三个文件的方法,其内容是 f1 和 f2 内容副本的串联(按该顺序(?
目前我只有足够的代码将一个文件复制到另一个文件:
public class annale2013q3{
public static void (File file,OutputStream os) throws {
BufferInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte b ;
do {
b = bis.read( );
if (b!= -1 ) os.write(b);
}
while (b!=-1);
bis.close( );
}
}
您需要将
文件集合传递给该方法并循环访问它们。在关闭在该方法中打开的文件时,您还应该更加小心。您可以使用 try-with-resource 语法来确保输入流已关闭。
public static void concat(Collection<File> files, OutputStream os) throws IOException {
for (File file : files) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
int b;
while ((b = bis.read()) != -1) {
os.write(b);
}
}
}
}