zoukankan      html  css  js  c++  java
  • 文件复制多份

    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();
            }
        }
    }
  • 相关阅读:
    Use Study Groups to Support Learning
    “开闭”原则(OpenClosed principle, OCP)
    我的E72i 开发
    conlution of daily work
    appstore相关查询链接
    sqlite3.0不支持的sql属性
    iOS sdk 运行时函数
    自动化测试部分
    ios下获取mac地址修正版
    修改mac os host
  • 原文地址:https://www.cnblogs.com/moris5013/p/11535797.html
Copyright © 2011-2022 走看看