美文网首页iApp技术文章
Java快速复制拷贝文件方法

Java快速复制拷贝文件方法

作者: 抹茶NightSky | 来源:发表于2018-11-24 16:51 被阅读0次
    大家好是抹茶今天给大家带来一个java教程,快速拷贝拷贝文件方法。

    ·下列缓存拷贝方法
    使用缓存拷贝适合拷贝一些中等大小的文件。

        /*Copy
         *缓存拷贝
         *Params path 路径
         *Params newpath 新路径
         */
        public static void Copy(String path, String newpath) throws Exception{
            InputStream in = new FileInputStream(path);
            OutputStream os = new FileOutputStream(newpath);
            byte[] b = new byte[1024];
            int len = 0;
            while((len = in.read(b))!=-1)
                os.write(b,0,len);
            os.flush();
            os.close();
            in.close();
        }
    

    ·下列缓冲拷贝方法
    使用缓冲拷贝适合拷贝一些大型的文件效率高。

        /*Copys
         *缓冲拷贝
         *Params path 路径
         *Params newpath 新路径
         */
        public static void Copys(String path, String newpath) throws Exception{
            InputStream in = new FileInputStream(path);
            OutputStream os = new FileOutputStream(newpath);
            BufferedInputStream bin = new BufferedInputStream(in);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            byte[] b = new byte[1024];
            int len = 0;
            while((len = bin.read(b))!=-1)
                bos.write(b,0,1024);
            bos.flush();
            bos.close();
            bin.close();
        }
    

    Demo

    //Demo
    public class Copy
    {
        public static void main(String[] args) throws Exception
        {
            long starttime = System.currentTimeMillis();
            Copy("C:\\lly.zip","D:\\lly.zip.bak");
            System.out.println(System.currentTimeMillis()-starttime+"ms");
            starttime = System.currentTimeMillis();
            Copys("C:\\lly.zip","D:\\lly.zip");
            System.out.println(System.currentTimeMillis()-starttime+"ms");
        }
        /*Copy
         *缓存拷贝
         *Params path 路径
         *Params newpath 新路径
         */
        public static void Copy(String path, String newpath) throws Exception{
            InputStream in = new FileInputStream(path);
            OutputStream os = new FileOutputStream(newpath);
            byte[] b = new byte[1024];
            int len = 0;
            while((len = in.read(b))!=-1)
                os.write(b,0,len);
            os.flush();
            os.close();
            in.close();
        }
        /*Copys
         *缓冲拷贝
         *Params path 路径
         *Params newpath 新路径
         */
        public static void Copys(String path, String newpath) throws Exception{
            InputStream in = new FileInputStream(path);
            OutputStream os = new FileOutputStream(newpath);
            BufferedInputStream bin = new BufferedInputStream(in);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            byte[] b = new byte[1024];
            int len = 0;
            while((len = bin.read(b))!=-1)
                bos.write(b,0,1024);
            bos.flush();
            bos.close();
            bin.close();
        }
    }
    

    相关文章

      网友评论

        本文标题:Java快速复制拷贝文件方法

        本文链接:https://www.haomeiwen.com/subject/jdjyqqtx.html