Android中的文件复制--视频和图片复制

public class FileOpreateUtils {/*** * @param fromFile 被复制的文件* @param toFile 复制的目录文件* @param rewrite 是否重新创建文件* * <p>文件的复制操作方法*/public static void copyfile(File fromFile, File toFile,Boolean rewrite ){if(!fromFile.exists()){return;}if(!fromFile.isFile()){return;}if(!fromFile.canRead()){return;}if(!toFile.getParentFile().exists()){toFile.getParentFile().mkdirs();}if(toFile.exists() && rewrite){toFile.delete();}try {FileInputStream fosfrom = new FileInputStream(fromFile);FileOutputStream fosto = new FileOutputStream(toFile);byte[] bt = new byte[1024];int c;while((c=fosfrom.read(bt)) > 0){fosto.write(bt,0,c);}//关闭输入、输出流fosfrom.close();fosto.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

不考虑多线程优化,单线程文件复制最快的方法是(文件越大该方法越有优势,一般比常用方法快30+%):

[java] view plaincopy
  1. private static void nioTransferCopy(File source, File target) {
  2. FileChannel in = null;
  3. FileChannel out = null;
  4. FileInputStream inStream = null;
  5. FileOutputStream outStream = null;
  6. try {
  7. inStream = new FileInputStream(source);
  8. outStream = new FileOutputStream(target);
  9. in = inStream.getChannel();
  10. out = outStream.getChannel();
  11. in.transferTo(0, in.size(), out);
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. } finally {
  15. close(inStream);
  16. close(in);
  17. close(outStream);
  18. close(out);
  19. }
  20. }

如果需要监测复制进度,可以用第二快的方法(留意buffer的大小,对速度有很大影响):

[c-sharp] view plaincopy
  1. private static void nioBufferCopy(File source, File target) {
  2. FileChannel in = null;
  3. FileChannel out = null;
  4. FileInputStream inStream = null;
  5. FileOutputStream outStream = null;
  6. try {
  7. inStream = new FileInputStream(source);
  8. outStream = new FileOutputStream(target);
  9. in = inStream.getChannel();
  10. out = outStream.getChannel();
  11. ByteBuffer buffer = ByteBuffer.allocate(4096);
  12. while (in.read(buffer) != -1) {
  13. buffer.flip();
  14. out.write(buffer);
  15. buffer.clear();
  16. }
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. } finally {
  20. close(inStream);
  21. close(in);
  22. close(outStream);
  23. close(out);
  24. }
  25. }

常用的方法1是:

[c-sharp] view plaincopy
  1. private static void customBufferBufferedStreamCopy(File source, File target) {
  2. InputStream fis = null;
  3. OutputStream fos = null;
  4. try {
  5. fis = new BufferedInputStream(new FileInputStream(source));
  6. fos = new BufferedOutputStream(new FileOutputStream(target));
  7. byte[] buf = new byte[4096];
  8. int i;
  9. while ((i = fis.read(buf)) != -1) {
  10. fos.write(buf, 0, i);
  11. }
  12. }
  13. catch (Exception e) {
  14. e.printStackTrace();
  15. } finally {
  16. close(fis);
  17. close(fos);
  18. }
  19. }

常用的方法2是:

[c-sharp] view plaincopy
  1. private static void customBufferStreamCopy(File source, File target) {
  2. InputStream fis = null;
  3. OutputStream fos = null;
  4. try {
  5. fis = new FileInputStream(source);
  6. fos = new FileOutputStream(target);
  7. byte[] buf = new byte[4096];
  8. int i;
  9. while ((i = fis.read(buf)) != -1) {
  10. fos.write(buf, 0, i);
  11. }
  12. }
  13. catch (Exception e) {
  14. e.printStackTrace();
  15. } finally {
  16. close(fis);
  17. close(fos);
  18. }
  19. }

延伸:

在Java编程中,复制文件的方法有很多,而且经常要用到。我以前一直是缓冲输入输出流来实现的(绝大多数人都是如此),近来在研究JDK文档时发现,用文件通道(FileChannel)来实现文件复制竟然比用老方法快了近三分之一。下面我就来介绍一下如何用文件通道来实现文件复制,以及在效率上的对比

  1. 用文件通道的方式来进行文件复制

     /**

        * 使用文件通道的方式复制文件

        * 

        * @param s

        *            源文件

        * @param t

        *            复制到的新文件

        */

    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();

                }

            }

        }

  2. 与普通的缓冲输入输出流的复制效率的对比

    普通的缓冲输入输出流代码:

    测试代码:

    输出结果:

  3. 由此可见,FileChannel复制文件的速度比BufferedInputStream/BufferedOutputStream复制文件的速度快了近三分之一。在复制大文件的时候更加体现出FileChannel的速度优势。而且FileChannel是多并发线程安全的。

Java: 复制文件最快、高效率的方法相关推荐

  1. JAVA复制文件夹的第二种方法

    包含复制所有目录和文件 package b;import java.io.*;public class Test01 {public static void main(String[] args) t ...

  2. java 复制文件_Java中复制文件的4种方法

    Java拷贝文件是一种非常常见的操作.但是java.io.File类没有任何快捷方法可以将文件从源复制到目标文件.在这里,我们将了解学习可以在java中复制文件的四种不同方法. 方法一:使用Strea ...

  3. java复制文件_java多种文件复制方式以及效率比较

    1.背景 java复制文件的方式其实有很多种,可以分为 - 传统的字节流读写复制FileInputStream,FileOutputStream,BufferedInputStream,Buffere ...

  4. java.util.zip 用法,Java压缩文件工具类ZipUtil使用方法代码示例

    本文实例通过Java的Zip输入输出流实现压缩和解压文件,前一部分代码实现获取文件路径,压缩文件名的更改等,具体如下: package com.utility.zip; import java.io. ...

  5. java复制文件的4种方式及拷贝文件到另一个目录下与删除单个文件和删除整个文件夹

    文章目录 1.使用FileStreams复制 2.使用FileChannel复制 3.使用Commons IO复制 4.使用Java7的Files类复制 5.下面看下java拷贝文件到另一个目录下的实 ...

  6. Java程序片:Java复制文件

    第一种方法(输入输出流): public void copyFile(File fromFile,File toFile) throws Exception{FileInputStream in=ne ...

  7. windows server2008无法将本地文件复制到远程计算机,windows2008/2012无法从本地复制文件到远程服务器处理方法...

    问题现象:远程登录到服务器后,从本地电脑复制文件无法在服务器里面粘贴(在服务器里面粘贴后是之前在服务器内部复制的信息)确认在远程连接工具中勾选了剪贴板.如图 问题原因:可能是 rdpclip.exe ...

  8. 安装程序无法复制文件 Atapi.sys的解决方法

    试图安装 Windows XP Service Pack 2.Windows XP Tablet PC Edition 2005 或 Windows Server 2003 Service Pack ...

  9. Java 复制文件夹及文件

    这里用到了两种复制文件夹及文件的方法 方法1:逐层复制文件夹&文件 参考博客1 @Controller public class UploadController {@Value(" ...

最新文章

  1. 软工实践原型设计——PaperRepositories
  2. 获取数组第N个元素的方法
  3. c语言程序做四则运算还要余数,大整数四则运算 高质量C语言程序.doc
  4. QIIME 2教程. 18序列双端合并read-joining(2020.11)
  5. 在日志文件中输出当前时间
  6. python程序设计教材浅显易懂_这些python自学技巧,你不会?
  7. javascript --- 文件上传即时预览 闭包实现多图片即时预览
  8. Linux下Zend Framework的“Invalid Controller Specified”问题
  9. windows server上存储提示“由于管理员设置的策略,该磁盘处于脱机状态”
  10. 如何让Mac在 Finder 顶部显示完整的文件路径
  11. 解决IIS 下ASP程序错误只显示500,不显示具体错误描述的问题!
  12. java计算机毕业设计高考填报信息系统源码+数据库+系统+lw文档+部署
  13. 华三交换机怎么关闭445端口通信
  14. 参考文献编号[9]之后出现空格解决方法
  15. 前端面试题,速看webP,把握住网页提速小细节!亲测可用!
  16. 曼哈顿算法公式_距离计算方法总结
  17. pytorch锁死在dataloader(训练时卡死)
  18. Celery异步任务
  19. Rocket MQ发送消息报错: service not available now
  20. sm缩写代表什么意思_什么是量比?个股量比代表什么意思

热门文章

  1. “万维网之父”发文阐述其下一个网络时代:将数据与应用分离,互联网去中心化正在路上...
  2. 哥伦比亚大学AI实验室主任Hod Lipson:阻碍无人驾驶技术发展的7个误区
  3. 量子计算陷入难解困境,未来发展何去何从?
  4. Uber致人死亡或为自动驾驶肇事责任 没有判例可循
  5. 总监调岗至前台,企业被判赔偿26万,法院:“侮辱性调岗”违法!
  6. Chrome 94 加入网页开发新技术,或有助于提高云游戏体验
  7. 内部黑客讲述:Twitter 史上最大规模攻击事件始末!
  8. 用Java实现Stream流处理中的滑窗
  9. 百度首席科学家吴恩达谈百度人工智能项目进展情况
  10. Flask入门 表单Flask-wtf form原生与Bootstrap渲染(七)