Re: [問題] 請問書上案例編譯錯誤找不到問題!?

看板java作者 (godfat 真常)時間18年前 (2007/09/15 01:29), 編輯推噓3(300)
留言3則, 3人參與, 最新討論串3/5 (看更多)
p 幣不夠用,小賺一下... ※ 引述《qrtt1 (愚人)》之銘言: : 據 A 君說,他印出來的結果都是被判斷為檔案不是目錄 : import java.io.File; : public class right { : public static void main(String[] args) { : String path = "C:\\Program Files\\Java\\jdk1.6.0"; : File rootPath = new File(path); : String[] stringArray = rootPath.list(); : File[] fileArray = rootPath.listFiles(); : assert stringArray.length == fileArray.length; : for(int i=0; i<stringArray.length; i++) { : boolean isSame = fileArray[i].isDirectory() == new : File(stringArray[i]).isDirectory(); ^^^^^^^^^^^^^^ 我不清楚 java 這些 api 效果如何(因為很少寫也懶得試,我對語言本身比較有興趣) 但是依照輸出結果,我猜這邊其實是有點問題的, 因為他該怎麼知道他是要打開 directory, 或是產生一個叫那個名字的檔案? public File(String pathname) Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname. Parameters: pathname - A pathname string 節自 java api doc, 沒寫! 是否要產生 directory 最後路徑上要加上 / or \\ ? 我懶得試了... : System.out.println(isSame +" : "+fileArray[i].getName()); : } : } : } : ======================================================================= : false : bin : true : COPYRIGHT : false : db : false : demo : false : include : false : jre : false : lib : true : LICENSE : true : LICENSE.rtf : true : README.html : true : README_ja.html : true : README_zh_CN.html : false : sample : true : src.zip : true : THIRDPARTYLICENSEREADME.txt : ========================================================================= : 有些目錄會有判定不一致的情況 : 看來用 listFiles 比較準,但是很傷資源 上面看起來都是碰到 directory 時才有閃失的, 所以猜測 new File(...) 不會打開已存在目錄, 而是產生同名檔案... 如果真的是「不準」的話,也未免太慘了點 @_@ -- 「行け!Loki!」(rocky ロッキー) -Gurumin ぐるみん 王子? XD -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.135.28.18

09/15 01:37, , 1F
產生檔案有別的指令吧 :~ 它只是產生一個有路徑的虛擬檔案
09/15 01:37, 1F

09/15 01:55, , 2F
虛擬檔案或實際檔案都不會是目錄啊
09/15 01:55, 2F

09/15 01:57, , 3F
只在某些目錄有問題, 不是都有問題, 像以c:\為 base 則正常
09/15 01:57, 3F
文章代碼(AID): #16wiK2QY (java)
討論串 (同標題文章)
文章代碼(AID): #16wiK2QY (java)