public class FileCopy {
public static void main(String[] args) throws Exception {
File f1 = new File("D:\\test\\demo.doc");
String path = "D:\\test\\";
System.out.print("请输入要复制的文件个数:");
Scanner sc = new Scanner(System.in);
int cnt = sc.nextInt();
List<String> list = new ArrayList<String>();
for (int i = 0; i < cnt; i++) {
System.out.print("请输入第" + (i + 1) + "个文件名:");
String newName = sc.next();
System.out.println("第" + (i + 1) + "个文件的名字为:" + newName + ".doc");
list.add(path + newName + ".doc");
}
list.stream().forEach(x -> System.out.println(x));
copyToMultipleFiles(f1, list.toArray(new String[0]));
}
public static void copyToMultipleFiles(File inFile, String[] outFiles) throws IOException {
OutputStream[] outStreams = new OutputStream[outFiles.length];
try {
for (int i = 0; i < outFiles.length; i++)
outStreams[i] = new FileOutputStream(outFiles[i]);
try (InputStream inStream = new FileInputStream(inFile)) {
byte[] buf = new byte[16384];
for (int len; (len = inStream.read(buf)) > 0;)
for (OutputStream outStream : outStreams)
outStream.write(buf, 0, len);
}
} finally {
for (OutputStream outStream : outStreams)
if (outStream != null)
outStream.close();
}
}
}