CNET中国旗舰网站

ZDNet China | CNET科技资讯网 | 政府采购 | 行业网站联盟





 
标题: [原创] SCJP考试试题解析五
mesum
超级会员
Rank: 4



UID 321006
精华 0
积分 1811
帖子 73
威望 722
ZD币 160 元
阅读权限 90
注册 2008-4-21
状态 离线
  楼主
发表于 2008-6-3 08:27  资料  个人空间  短消息  加为好友 
开发者在线

SCJP考试试题解析五

SCJP考试试题解析五

我的QQ号:2535279
我的QQ群:58591592

www.javaedu.com.cn





The doesFileExist method takes an array of directory names representing a path from the root filesystem and  a filename.The

method returns true if the file exists,false if does not.

Place the code fragments in position to complete this method.

//insert here A
for(String dir:directories){
    //insert here B
}

//insert here C

//insert here D

Code Fragments

1.path = path.getSubdirectory(dir);
2.return !file.inNew();
3.return (file!=null);
4.String path="";
5.path = path.getFile(filename);
6.File path = new File("");
7.return file.exist();
8.return path.isFile();
9.File file = new File(path,filename);
10.path = new File(path,dir);
11.File path = new File(File.separator);
12.path = path+FileSeparator+dir;

当然了,要想做出这道题目,首先要知道这个程序段想要做什么,要完成什么任务.

这是从doesFileExist方法中截取的一段代码,doesFileExist方法完成的主要功能是:这个方法会获得一个字符串数组和一个文件名.这个字符

串数组存放的是从根目录开始的一个路径.doesFileExist方法中,如果文件存在,返回true,如果不存在,返回false;

既然知道了要做什么事情,我们就很清楚的知道我们要去做什么事情了:

首先,我们要先把字符串数组中的元素依次取出来,把它们连接到一起,形成一个完整的绝对路径.然后,创建用这个路径和已经得到的文件名做

参加,生成一个File对象,利用File对象的exist()方法,来判断这个文件是否存在.

那么,我们的答案是什么呢:
A:4
B:12
C:9
D:7

我也试着写了一下测试类

import java.io.File;

public class Tester {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String[] directories = new String[] { "D:", "homework", "d0427" };
        String filename = "table.xml";

        System.out.println(doesFileExist(directories, filename));

    }

    public static boolean doesFileExist(String[] directories, String filename) {

        String path = "";
        for (String dir : directories) {
            path = path + File.separator + dir;
        }
        File file = new File(path, filename);

        return file.exists();
    }

}

在我的电脑里,D:\homework\d0427确实存在着一个文件table.xml,所以,返回值为true




顶部
wgswgswgs
中级会员
Rank: 1


UID 329805
精华 0
积分 472
帖子 40
威望 216
ZD币 72 元
阅读权限 30
注册 2008-5-27
状态 离线
  沙发
发表于 2008-6-3 09:43  资料  个人空间  短消息  加为好友 
路过学习中...

我虽然不太懂JAVA,但觉得楼主写的不错,帮忙顶了




顶部
热点频道推荐: C/S开发| 数据库| WEB开发| 嵌入式| 项目管理|
 



当前时区 GMT+8, 现在时间是 2009-7-5 08:39

  Powered by Discuz! 5.5.0 © 2001-2007 Comsenz Inc.
Processed in 0.063276 second(s), 4/3 queries

清除 Cookies - 联系我们 - ZDNetChina中文社区 - 无图版