作者godfat (godfat 真常)
看板java
標題Re: [問題] 請問書上案例編譯錯誤找不到問題!?
時間Sat Sep 15 01:29:35 2007
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
1F:推 TonyQ:產生檔案有別的指令吧 :~ 它只是產生一個有路徑的虛擬檔案 09/15 01:37
2F:推 godfat:虛擬檔案或實際檔案都不會是目錄啊 09/15 01:55
3F:推 qrtt1:只在某些目錄有問題, 不是都有問題, 像以c:\為 base 則正常 09/15 01:57