Java快速复制拷贝文件方法

大家好是抹茶今天给大家带来一个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();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,950评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,466评论 2 59
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,793评论 1 32
  • 最近公司发布了一款新产品,没几天,就有朋友跟我说:你们的东西被山寨了。我搜了搜,发现确实是有一款很类似的产品。然后...
    JasonHuang阅读 4,547评论 0 15
  • 你我计算太多爱的太过总是太自我
    丹木子一九九八阅读 1,248评论 0 0