JAVA复制文件最快速的方法:用文件通道的方式来进行文件复制

来源:互联网 发布:网络伤感情歌2016 编辑:程序博客网 时间:2024/06/05 01:15

JAVA复制文件最快速的方法:用文件通道的方式来进行文件复制,直接贴代码了,以后备查

转自

http://jingyan.baidu.com/article/ff4116259c2d7712e4823780.html

    public void fileChannelCopy(File s, File t) {        FileInputStream fi = null;        FileOutputStream fo = null;        FileChannel in = null;        FileChannel out = null;        try {            fi = new FileInputStream(s);            fo = new FileOutputStream(t);            in = fi.getChannel();//得到对应的文件通道            out = fo.getChannel();//得到对应的文件通道            in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                fi.close();                in.close();                fo.close();                out.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }


0 0
原创粉丝点击