2014-11-04 89 views
1

我有下面的代碼,它顯示了一個沒有任何完整停止的文件夾,並且java成功檢測到它是一個文件夾,但是如果該文件夾中包含任何完整停止,檢測到它作爲一個文件夾或即使它存在無法訪問其中包含完全停止的文件夾

List<String> files = new ArrayList<String>(); 
files.add("F:/some dir/"); 
files.add("F:\\somedir\\some.other.directory.with.full.stops.in.it"); 
files.add("F:\\somedir\\some.other.directory.with.full.stops.in.it\\"); 
files.add("F:/somedir/some.other.directory.with.full.stops.in.it"); 
files.add("F:/somedir/some.other.directory.with.full.stops.in.it/"); 

files.add("F:\\somedir\\some.other.directory.with.full.stops.in.it\\sub directory"); 
files.add("F:\\somedir\\some.other.directory.with.full.stops.in.it\\sub directory\\"); 
files.add("F:/somedir/some.other.directory.with.full.stops.in.it/sub directory"); 
files.add("F:/somedir/some.other.directory.with.full.stops.in.it/sub directory//"); 

for(String filePath : files) 
{ 
    File file = new File(filePath); 
    System.out.println("===== " + filePath + " ===== "); 
    System.out.println("IsDirectory = " + file.isDirectory()); 
    System.out.println("IsFile = " + file.isFile()); 
} 

這裏甚至一個文件是在我的系統輸出(窗口8.1 64位JRE 1.8.0_25)

===== F:/some dir/ ===== 
IsDirectory = true 
IsFile = false 
===== F:\somedir\some.other.directory.with.full.stops.in.it ===== 
IsDirectory = false 
IsFile = false 
===== F:\somedir\some.other.directory.with.full.stops.in.it\ ===== 
IsDirectory = false 
IsFile = false 
===== F:/somedir/some.other.directory.with.full.stops.in.it ===== 
IsDirectory = false 
IsFile = false 
===== F:/somedir/some.other.directory.with.full.stops.in.it/ ===== 
IsDirectory = false 
IsFile = false 
===== F:\somedir\some.other.directory.with.full.stops.in.it\sub directory ===== 
IsDirectory = false 
IsFile = false 
===== F:\somedir\some.other.directory.with.full.stops.in.it\sub directory\ ===== 
IsDirectory = false 
IsFile = false 
===== F:/somedir/some.other.directory.with.full.stops.in.it/sub directory ===== 
IsDirectory = false 
IsFile = false 
===== F:/somedir/some.other.directory.with.full.stops.in.it/sub directory// ===== 
IsDirectory = false 
IsFile = false 
+0

您確定它存在嗎?看起來你已經刪除了'some dir'中的空間 - 是否會導致你的問題? – Jason 2014-11-04 03:20:37

+0

DId檢查磁盤上是否存在實際文件夾? – 2014-11-04 03:25:59

+0

上帝讓我發笑。請不要被冒犯。我笑,因爲我發生過同樣的事情。這是點嗎?斜槓?精氨酸!你可以用你的工作來簡化代碼,以幫助你找到bug,但有時候你真的只需要第二雙眼睛。 – CandiedOrange 2014-11-04 03:34:41

回答

1

試試這個 - 我已修復可能是錯別字的:

List<String> files = new ArrayList<String>(); 
files.add("F:/some dir/"); 
files.add("F:\\some dir\\some.other.directory.with.full.stops.in.it"); 
files.add("F:\\some dir\\some.other.directory.with.full.stops.in.it\\"); 
files.add("F:/some dir/some.other.directory.with.full.stops.in.it"); 
files.add("F:/some dir/some.other.directory.with.full.stops.in.it/"); 

files.add("F:\\some dir\\some.other.directory.with.full.stops.in.it\\sub directory"); 
files.add("F:\\some dir\\some.other.directory.with.full.stops.in.it\\sub directory\\"); 
files.add("F:/some dir/some.other.directory.with.full.stops.in.it/sub directory"); 
files.add("F:/some dir/some.other.directory.with.full.stops.in.it/sub directory//"); 

for(String filePath : files) 
{ 
    File file = new File(filePath); 
    System.out.println("===== " + filePath + " ===== "); 
    System.out.println("IsDirectory = " + file.isDirectory()); 
    System.out.println("IsFile = " + file.isFile()); 
} 
+0

是錯別字...太集中在結束文件夾註意到錯誤。感謝您的注意。 – user3045798 2014-11-04 03:28:11