A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张绍成 黑马帝   /  2011-12-23 21:12  /  2249 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 tianshan20081 于 2011-12-25 15:31 编辑

怎么样的拷贝文件方式才是高效的;
我这样写高效吗?
代码:

  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.sql.Timestamp;
  6. //利用 FileInputStream 来实现输入,利用FileOutputStream 来实现输出,用以实现文件的复制
  7. public class FileCopy {
  8.         public static void main(String[] args) throws Exception {
  9.                 FileInputStream fis = null ;
  10.                 FileOutputStream fos = null ;
  11.                 try {
  12.                         //创建字节输入流
  13.                         fis = new FileInputStream("d:\\JDK.6.Documentation.chm");
  14.                         //创建字节输出流
  15.                         fos = new FileOutputStream("d:\\download\\java help.chm");
  16.                         byte[] bbuf = new byte[1024];
  17.                         int len = 0 ;
  18.                         Timestamp start = new Timestamp(System.currentTimeMillis());
  19.                         System.out.println(start);
  20.                         //循环重输入流中读取数据
  21.                         while((len=fis.read(bbuf))!=-1){
  22.                                 //每读取一次就将数据写入一次,读了多少就写多少
  23.                                 fos.write(bbuf, 0, len);
  24.                         }
  25.                         Timestamp end = new Timestamp(System.currentTimeMillis());
  26.                         System.out.println(end);
  27.                 } catch (IOException e) {
  28.                         // TODO Auto-generated catch block
  29.                         e.printStackTrace();
  30.                 }finally{
  31.                         // 利用 finally 关闭输入输出流。finally 是程序必须执行的部分
  32.                         if(fis!=null){
  33.                                 fis.close();
  34.                         }
  35.                         if(fos!=null){
  36.                                 fos.close();
  37.                         }
  38.                 }
  39.         }
  40. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
吴上储 + 1

查看全部评分

3 个回复

倒序浏览
本帖最后由 袁泽宇 于 2011-12-23 21:40 编辑

第一种方法,是采用普通的IO,Buffered流,速度一般;
另一种是采用Channel流的transfer方法,速度超快。
  1. import java.io.FileInputStream;
  2. import java.io.FileOutputStream;
  3. import java.nio.channels.FileChannel;


  4. public class TestTransfer {


  5. public static void main(String[] args) throws Exception {

  6.   FileInputStream fis = new FileInputStream("c:\\abc.txt");

  7.   FileOutputStream fos = new FileOutputStream("c:\\123.txt");

  8.   FileChannel fc1 = fis.getChannel();

  9.   FileChannel fc2 = fos.getChannel();

  10.   fc2.transferFrom(fc1, 0, fc1.size());

  11.   fc1.close();

  12.   fc2.close();

  13.   fis.close();

  14.   fos.close();

  15. }

  16. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
吴上储 + 2

查看全部评分

回复 使用道具 举报

  1. public static void fileCopy( File in, File out ) throws IOException
  2. {
  3.     FileChannel inChannel = new FileInputStream( in ).getChannel();
  4.     FileChannel outChannel = new FileOutputStream( out ).getChannel();
  5.     try
  6.     {
  7.         inChannel.transferTo(0, inChannel.size(), outChannel);      
  8.         // original -- apparently has trouble copying large files on Windows
  9.         // magic number for Windows, 64Mb - 32Kb)
  10.         int maxCount = (64 * 1024 * 1024) - (32 * 1024);
  11.         long size = inChannel.size();
  12.         long position = 0;
  13.         while ( position < size )
  14.         {
  15.                 position += inChannel.transferTo( position, maxCount, outChannel );
  16.         }
  17.     }
  18.     finally
  19.     {
  20.         if ( inChannel != null )
  21.         {
  22.                 inChannel.close();
  23.         }
  24.         if ( outChannel != null )
  25.         {
  26.                 outChannel.close();
  27.         }
  28.     }
  29. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
吴上储 + 1

查看全部评分

回复 使用道具 举报
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;

  5. public class DirDemo {

  6.         private static File getDir() throws IOException {
  7.                 System.out.println("请输入路径:");
  8.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  9.                 String line = br.readLine();
  10.                 File f = new File(line);
  11.                 return f;
  12.         }
  13. }


  14. public class Test3 {
  15.         public static void main(String[] args) throws IOException {
  16.                
  17.                 File file = DirUtil.getDir();
  18.                 File dest = DirUtil.getDir();
  19.                 fileCopy(file,dest);
  20.         }
  21.         public static void fileCopy(File src, File dest) throws IOException {
  22.                 File newDest = new File(dest, src.getName());
  23.                 newDest.mkdir();

  24.                 File[] fi = src.listFiles();
  25.                 for (File f : fi) {
  26.                         if (f.isDirectory()) {
  27.                                 fileCopy(f, newDest);
  28.                         } else {
  29.                                 FileInputStream fs = new FileInputStream(f);
  30.                                 FileOutputStream fo = new FileOutputStream(new File(newDest, f.getName()));
  31.                                 byte[] b = new byte[1024];
  32.                                 int len;
  33.                                 while ((len = fs.read(b)) != -1) {
  34.                                         fo.write(b, 0, len);
  35.                                 }
  36.                                 fs.close();
  37.                                 fo.close();
  38.                         }
  39.                 }
  40.         }

  41. }

复制代码

评分

参与人数 1技术分 +1 收起 理由
吴上储 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马