Java實現(xiàn)文件拷貝的4種方法

          第一種方法:古老的方式

           public static long forJava(File f1,File f2) throws Exception{
            long time=new Date().getTime();
            int length=2097152;
            FileInputStream in=new FileInputStream(f1);
            FileOutputStream out=new FileOutputStream(f2);
            byte[] buffer=new byte[length];
            while(true){
             int ins=in.read(buffer);
             if(ins==-1){
              in.close();
              out.flush();
              out.close();
              return new Date().getTime()-time;
             }else
              out.write(buffer,0,ins);
            }
           }
          方法的2參數(shù)分別是原始文件,和拷貝的目的文件.這里不做過多介紹.

          實現(xiàn)方法很簡單,分別對2個文件構(gòu)建輸入輸出流,并且使用一個字節(jié)數(shù)組作為我們內(nèi)存的緩存器, 然后使用流從f1 中讀出數(shù)據(jù)到緩存里,在將緩存數(shù)據(jù)寫到f2里面去.這里的緩存是2MB的字節(jié)數(shù)組

          第2種方法:使用NIO中的管道到管道傳輸

              public static long forTransfer(File f1,File f2) throws Exception{
                  long time=new Date().getTime();
                  int length=2097152;
                  FileInputStream in=new FileInputStream(f1);
                  FileOutputStream out=new FileOutputStream(f2);
                  FileChannel inC=in.getChannel();
                  FileChannel outC=out.getChannel();
                  int i=0;
                  while(true){
                      if(inC.position()==inC.size()){
                          inC.close();
                          outC.close();
                          return new Date().getTime()-time;
                      }
                      if((inC.size()-inC.position())<20971520)
                          length=(int)(inC.size()-inC.position());
                      else
                          length=20971520;
                      inC.transferTo(inC.position(),length,outC);
                      inC.position(inC.position()+length);
                      i++;
                  }
              }
          實現(xiàn)方法:在第一種實現(xiàn)方法基礎(chǔ)上對輸入輸出流獲得其管道,然后分批次的從f1的管道中像f2的管道中輸入數(shù)據(jù)每次輸入的數(shù)據(jù)最大為2MB

          方法3:內(nèi)存文件景象寫(讀文件沒有使用文件景象,有興趣的可以回去試試,,我就不試了,估計會更快)

              public static long forImage(File f1,File f2) throws Exception{
                  long time=new Date().getTime();
                  int length=2097152;
                  FileInputStream in=new FileInputStream(f1);
                  RandomAccessFile out=new RandomAccessFile(f2,"rw");
                  FileChannel inC=in.getChannel();
                  MappedByteBuffer outC=null;
                  MappedByteBuffer inbuffer=null;
                  byte[] b=new byte[length];
                  while(true){
                      if(inC.position()==inC.size()){
                          inC.close();
                          outC.force();
                          out.close();
                          return new Date().getTime()-time;
                      }
                      if((inC.size()-inC.position())<length){
                          length=(int)(inC.size()-inC.position());
                      }else{
                          length=20971520;
                      }
                      b=new byte[length];
                      inbuffer=inC.map(MapMode.READ_ONLY,inC.position(),length);
                      inbuffer.load();
                      inbuffer.get(b);
                      outC=out.getChannel().map(MapMode.READ_WRITE,inC.position(),length);
                      inC.position(b.length+inC.position());
                      outC.put(b);
                      outC.force();
                  }
              }
          實現(xiàn)方法:跟傷2個例子不一樣,這里寫文件流沒有使用管道而是使用內(nèi)存文件映射(假設(shè)文件f2在內(nèi)存中).在循環(huán)中從f1的管道中讀取數(shù)據(jù)到字節(jié)數(shù)組里,然后在像內(nèi)存映射的f2文件中寫數(shù)據(jù).

          第4種方法:管道對管道

              public static long forChannel(File f1,File f2) throws Exception{
                  long time=new Date().getTime();
                  int length=2097152;
                  FileInputStream in=new FileInputStream(f1);
                  FileOutputStream out=new FileOutputStream(f2);
                  FileChannel inC=in.getChannel();
                  FileChannel outC=out.getChannel();
                  ByteBuffer b=null;
                  while(true){
                      if(inC.position()==inC.size()){
                          inC.close();
                          outC.close();
                          return new Date().getTime()-time;
                      }
                      if((inC.size()-inC.position())<length){
                          length=(int)(inC.size()-inC.position());
                      }else
                          length=2097152;
                      b=ByteBuffer.allocateDirect(length);
                      inC.read(b);
                      b.flip();
                      outC.write(b);
                      outC.force(false);
                  }
              }
          這里實現(xiàn)方式與第3種實現(xiàn)方式很類似,不過沒有使用內(nèi)存影射。

          posted on 2009-10-24 00:41 飛熊 閱讀(424) 評論(0)  編輯  收藏 所屬分類: java

          <2009年10月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 阜宁县| 惠州市| 印江| 错那县| 花莲市| 双辽市| 敦煌市| 开封县| 循化| 甘泉县| 泰宁县| 托克逊县| 如东县| 东阿县| 海晏县| 桃园市| 新沂市| 尤溪县| 木兰县| 富川| 隆子县| 湘潭市| 建阳市| 通榆县| 武陟县| 高淳县| 乌海市| 图木舒克市| 怀来县| 玛纳斯县| 神木县| 永修县| 定结县| 嘉善县| 基隆市| 商都县| 隆安县| 思茅市| 五大连池市| 彭水| 汉沽区|