作者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