Java复制单个文件和文件夹
2014-07-16 16:00
423 查看
/** * 复制单个文件 * @param oldPath String 原文件路径 如:c:/fqf.txt * @param newPath String 复制后路径 如:f:/fqf.txt * @return boolean */ public void copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { //文件存在时 InputStream inStream = new FileInputStream(oldPath); //读入原文件 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //字节数 文件大小 System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { System.out.println("复制单个文件操作出错"); e.printStackTrace(); } } /** * 复制整个文件夹内容 * @param oldPath String 原文件路径 如:c:/fqf * @param newPath String 复制后路径 如:f:/fqf/ff * @return boolean */ public void copyFolder(String oldPath, String newPath) { try { (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 File a=new File(oldPath); String[] file=a.list(); File temp=null; for (int i = 0; i < file.length; i++) { if(oldPath.endsWith(File.separator)){ temp=new File(oldPath+file[i]); } else{ temp=new File(oldPath+File.separator+file[i]); } if(temp.isFile()){ FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString()); byte[] b = new byte[1024 * 5]; int len; while ( (len = input.read(b)) != -1) { output.write(b, 0, len); } output.flush(); output.close(); input.close(); } if(temp.isDirectory()){//如果是子文件夹 copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); } } } catch (Exception e) { System.out.println("复制整个文件夹内容操作出错"); e.printStackTrace(); } }
<h1>Java 复制文件的高效方法</h1> 在Java编程中,复制文件的方法有很多,而且经常要用到。我以前一直是缓冲输入输出流来实现的(绝大多数人都是如此),近来在研究JDK文档时发现,用文件通道(FileChannel)来实现文件复制竟然比用老方法快了近三分之一。下面我就来介绍一下如何用文件通道来实现文件复制,以及在效率上的对比 用文件通道的方式来进行文件复制 /** * 使用文件通道的方式复制文件 * * @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(); } } } 与普通的缓冲输入输出流的复制效率的对比 普通的缓冲输入输出流代码: 测试代码: 输出结果: 由此可见,FileChannel复制文件的速度比BufferedInputStream/BufferedOutputStream复制文件的速度快了近三分之一。在复制大文件的时候更加体现出FileChannel的速度优势。而且FileChannel是多并发线程安全的。
相关文章推荐
- java单个文件复制,递归删除、复制目录下所有内容,递归创建文件夹,.7z文件解压等
- java中复制文件和文件夹Demo,压缩单个文件夹Demo,压缩文件和文件夹Demo
- java复制单个文件代码~
- 复制一个文件夹中的所有文件和文件夹的java程序实现
- java复制文件、文件夹
- 今天看到了一个用java实现复制,剪切,删除文件或文件夹的示例,感觉不错,欢迎学习(带详细注释)
- java 实现文件/文件夹复制、删除、移动(一)
- JAVA将文件复制到指定的文件夹中(源码)
- Java中实现复制文件或文件夹——CopyUtil.java
- android-Java中实现复制文件或文件夹 - 随心
- java遍历文件夹并复制文件到指定目录
- java复制文件或文件夹-供自用他用
- java中实现复制文件和文件夹
- java复制文件或文件夹
- java 的 文件、文件夹 的建立、删除、复制以及移动等功能 操作
- Java中实现复制文件或文件夹
- ( 转载)纯Java文件操作工具,支持文件、文件夹的复制、删除、移动
- Java中实现复制文件或文件夹
- java 实现文件/文件夹复制、删除、移动(二)
- java复制文件或文件夹