<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    bt下載與小說520

    bt下載與小說520

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      16 隨筆 :: 0 文章 :: 6 評論 :: 0 Trackbacks

    以前我一直以為File#renameTo(File)方法與OS下面的 move/mv 命令是相同的,可以達到改名、移動文件的目的。不過后來經常發現問題,真的很bt,File#renameTo(File)方法會返回失敗(false),文件沒有移動,又查不出原因,再后來干脆棄用該方法,自己實現一個copy方法,問題倒是再也沒有出現過。

    昨天老板同學又遇到這個問題,File#renameTo(File)方法在windows下面工作的好好的,在linux下偶爾又失靈了。回到家我掃了一遍JDK中File#renameTo(File)方法的源代碼,發現它調用的是一個本地的方法(native method),無法再跟蹤下去。網上有人說該方法在window下是正常的,在linux下面是不正常的。這個很難說通,SUN不可能搞出這種平臺不一致的代碼出來啊。

    后面在SUN的官方論壇上看到有人提到這個問題“works on windows, don't work on linux”,后面有人回復說是“file systems”不一樣。究竟怎么不一樣呢?還是沒有想出來...

    后面在一個論壇里面發現了某人關于這個問題的闡述:
    In the Unix'esque O/S's you cannot renameTo() across file systems. This behavior is different than the Unix "mv" command. When crossing file systems mv does a copy and delete which is what you'll have to do if this is the case.

    The same thing would happen on Windows if you tried to renameTo a different drive, i.e. C: -> D:
    終于明白咯。

    做個實驗:

  • File sourceFile = new File("c:/test.txt");   
  • File targetFile1 = new File("e:/test.txt");   
  • File targetFile2 = new File("d:/test.txt");   
  • System.out.println("source file is exist? " + sourceFile.exists()   
  •     + ", source file => " + sourceFile);   
  • System.out.println(targetFile1 + " is exist? " + targetFile1.exists());   
  • System.out.println("rename to " + targetFile1 + " => "  
  •     + sourceFile.renameTo(targetFile1));   
  • System.out.println("source file is exist? " + sourceFile.exists()   
  •     + ", source file => " + sourceFile);   
  • System.out.println(targetFile2 + " is exist? " + targetFile2.exists());   
  • System.out.println("rename to " + targetFile2 + " => "  
  •     + sourceFile.renameTo(targetFile2));  



  • 注意看結果,從C盤到E盤失敗了,從C盤到D盤成功了。因為我的電腦C、D兩個盤是NTFS格式的,而E盤是FAT32格式的。所以從C到E就是上面文章所說的"file systems"不一樣。從C到D由于同是NTFS分區,所以不存在這個問題,當然就成功了。

    果然是不能把File#renameTo(File)當作move方法使用。

    可以考慮使用apache組織的commons-io包里面的FileUtils#copyFile(File,File)和FileUtils#copyFileToDirectory(File,File)方法實現copy的效果。至于刪除嘛,我想如果要求不是那么精確,可以調用File#deleteOnExit()方法,在虛擬機終止的時候,刪除掉這個目錄或文件。

    BTW:File是文件和目錄路徑名的抽象表示形式,所以有可能是目錄,千萬小心。
    下面我寫的一個實現方法

    /**
      * 使用FileChannel拷貝文件
      *
      * @param srcFile
      * @param destFile
      * @throws IOException
      */
     public static void copyUseChannel(File srcFile, File destFile)
       throws IOException {
      if ((!srcFile.exists()) || (srcFile.isDirectory())) {
       return;
      }

      if (!destFile.exists()) {
       createFile(destFile.getAbsolutePath());
      }

      FileChannel out = null;
      FileChannel in = null;
      try {
       out = new FileOutputStream(destFile).getChannel();
       in = new FileInputStream(srcFile).getChannel();
       ByteBuffer buffer = ByteBuffer.allocate(102400);
       int position = 0;
       int length = 0;
       while (true) {
        length = in.read(buffer, position);
        if (length <= 0) {
         break;
        }
        // System.out.println("after read:"+buffer);
        buffer.flip();
        // System.out.println("after flip:"+buffer);
        out.write(buffer, position);
        position += length;
        buffer.clear();
        // System.out.println("after clear:"+buffer);
       }

      } finally {
       if (out != null) {
        out.close();
       }
       if (in != null) {
        in.close();
       }
      }
     }

    posted on 2008-10-27 10:15 bt下載 閱讀(1677) 評論(2)  編輯  收藏

    評論

    # re: java的File#renameTo(File)方法的陷井 2008-10-27 11:24 temper
    收藏。最近正好要用到這個方法,回去先驗證一下  回復  更多評論
      

    # re: java的File#renameTo(File)方法的陷井 2008-10-27 12:37 moron128
    也遇到過這個問題
    還有目標路徑下如有同名文件時好像無法覆蓋
    需要事先判斷  回復  更多評論
      


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品中文字幕无乱码| 亚洲а∨天堂久久精品9966| 亚洲av无码一区二区三区观看| 国产精品无码亚洲一区二区三区| eeuss在线兵区免费观看| 中文字幕无码免费久久99| 亚洲精品tv久久久久久久久久| 亚洲激情在线观看| 韩国亚洲伊人久久综合影院| 免费无码成人AV在线播放不卡| 国产美女做a免费视频软件| 内射少妇36P亚洲区| 免费手机在线看片| 国内精品免费麻豆网站91麻豆 | 亚洲精品无码99在线观看| 亚洲视频在线免费播放| 免费国产在线精品一区| a拍拍男女免费看全片| 国产亚洲午夜高清国产拍精品 | 无码人妻一区二区三区免费手机| 亚洲另类激情专区小说图片| 亚洲国产成+人+综合| 9i9精品国产免费久久| 日韩一级在线播放免费观看| 亚洲精品456在线播放| 久久精品免费网站网| 在线视频免费观看www动漫| 久久亚洲伊人中字综合精品| 精品女同一区二区三区免费播放| 久久久久久毛片免费播放| 久久夜色精品国产亚洲av| 亚洲性色精品一区二区在线| 99久久99久久免费精品小说| 久久久久噜噜噜亚洲熟女综合| 亚洲精品无码专区| 国产精品久久久久久久久免费| 亚洲一区二区三区影院| 深夜福利在线视频免费| 免费的一级片网站| 国产成人精品亚洲2020| 2019中文字幕免费电影在线播放|