File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chapter 6 from K&B question 12 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Chapter 6 from K&B question 12" Watch "Chapter 6 from K&B question 12" New topic

Chapter 6 from K&B question 12

kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
Hi ,
i have created a folder Cdir,created two subdirectories dir1,dir2 and placed file1.txt,file2.txt accordingly as per question no 12.

for those who not having book.


Compiled it proper for the first iteration path=dir1 arg[0]=file2.txt
file.exists is false thatis okie as there is no such file in that dir1.
but for the second iteration of for loop path=dir2 arg[0]=file2.txt i am getting false,this i cannot beleive as file2.txt is there in dir2?
also i need information why option c,d is wrong?

kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
i forgot to add the code properly.

class Directories {
static String [] dirs = {"dir1", "dir2"};
public static void main(String [] args) {
for (String d : dirs) {
// insert code 1 here
File file = new File(path, args[0]);
// insert code 2 here
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Answers C and D are wrong because, prefixing File.separator adds "/" (in unix) or "\" (in windows) to the means path is absolute one starts the the root of the file system, which doesn't have "dir1" and "dir2" in this case.

Paths can be referred in the File object in two ways. Relative and Absolute. If the path string starts with "/" or "\", then it is considered as absolute otherwise, relative.

In case of relative path string, the File object takes the current working directory (user.dir system variable) of the application and prefixes to the path string.

So make sure you are running the application from the CDir otherwise, Java app won't see the dir1 and dir2 folders.
I agree. Here's the link:
subject: Chapter 6 from K&B question 12
It's not a secret anymore!