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

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

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

    bt下載與小說(shuō)520

    bt下載與小說(shuō)520

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      16 隨筆 :: 0 文章 :: 6 評(píng)論 :: 0 Trackbacks

    以前我一直以為File#renameTo(File)方法與OS下面的 move/mv 命令是相同的,可以達(dá)到改名、移動(dòng)文件的目的。不過(guò)后來(lái)經(jīng)常發(fā)現(xiàn)問(wèn)題,真的很bt,File#renameTo(File)方法會(huì)返回失敗(false),文件沒(méi)有移動(dòng),又查不出原因,再后來(lái)干脆棄用該方法,自己實(shí)現(xiàn)一個(gè)copy方法,問(wèn)題倒是再也沒(méi)有出現(xiàn)過(guò)。

    昨天老板同學(xué)又遇到這個(gè)問(wèn)題,F(xiàn)ile#renameTo(File)方法在windows下面工作的好好的,在linux下偶爾又失靈了。回到家我掃了一遍JDK中File#renameTo(File)方法的源代碼,發(fā)現(xiàn)它調(diào)用的是一個(gè)本地的方法(native method),無(wú)法再跟蹤下去。網(wǎng)上有人說(shuō)該方法在window下是正常的,在linux下面是不正常的。這個(gè)很難說(shuō)通,SUN不可能搞出這種平臺(tái)不一致的代碼出來(lái)啊。

    后面在SUN的官方論壇上看到有人提到這個(gè)問(wèn)題“works on windows, don't work on linux”,后面有人回復(fù)說(shuō)是“file systems”不一樣。究竟怎么不一樣呢?還是沒(méi)有想出來(lái)...

    后面在一個(gè)論壇里面發(fā)現(xiàn)了某人關(guān)于這個(gè)問(wèn)題的闡述:
    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:
    終于明白咯。

    做個(gè)實(shí)驗(yàn):

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



  • 注意看結(jié)果,從C盤到E盤失敗了,從C盤到D盤成功了。因?yàn)槲业碾娔XC、D兩個(gè)盤是NTFS格式的,而E盤是FAT32格式的。所以從C到E就是上面文章所說(shuō)的"file systems"不一樣。從C到D由于同是NTFS分區(qū),所以不存在這個(gè)問(wèn)題,當(dāng)然就成功了。

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

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

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

    /**
      * 使用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) 評(píng)論(2)  編輯  收藏

    評(píng)論

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

    # re: java的File#renameTo(File)方法的陷井 2008-10-27 12:37 moron128
    也遇到過(guò)這個(gè)問(wèn)題
    還有目標(biāo)路徑下如有同名文件時(shí)好像無(wú)法覆蓋
    需要事先判斷  回復(fù)  更多評(píng)論
      


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲第一视频在线观看免费| 亚洲综合成人婷婷五月网址| 羞羞视频免费观看| 国产成人一区二区三区免费视频| 亚洲中文字幕乱码AV波多JI| 成年女人毛片免费播放视频m| 亚洲一区二区三区高清在线观看| 成人免费视频网址| 亚洲乱妇熟女爽到高潮的片| 日韩在线天堂免费观看| 国产午夜亚洲精品不卡免下载| 免费大香伊蕉在人线国产| 人人公开免费超级碰碰碰视频| 在线亚洲人成电影网站色www| 全黄大全大色全免费大片| 一区二区三区亚洲| 91免费精品国自产拍在线不卡| 亚洲日韩国产一区二区三区在线| 国产精品免费看香蕉| 久草免费福利在线| 日产亚洲一区二区三区| 成人在线视频免费| eeuss影院免费92242部| 亚洲午夜在线电影| 成人毛片免费观看视频大全| 牛牛在线精品观看免费正| 国产AV无码专区亚洲Av| 可以免费看黄的网站| 香港特级三A毛片免费观看| 久久精品国产亚洲网站| 美女视频黄a视频全免费| 日韩久久无码免费毛片软件| 亚洲av永久无码精品秋霞电影影院 | 有码人妻在线免费看片| 久久精品亚洲综合专区| 成人午夜性A级毛片免费| 国产永久免费高清在线| 亚洲综合无码无在线观看| 超清首页国产亚洲丝袜| 成人免费午夜无码视频| 亚洲一区二区三区免费|