Forums Register Login

Chapter 6 from K&B question 12

+Pie Number of slices to send: Send
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.


answers:A,B

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?


Regards
Kirba.
+Pie Number of slices to send: Send
i forgot to add the code properly.

import java.io.*;
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
}
}
}
+Pie Number of slices to send: Send
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.
We can walk to school together. And we can both read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1080 times.
Similar Threads
File Question
Doubt in K&B concerning question 12
Updated K&B 5 Errata - 4/16
I/O K&B Question
Question Regarding K & B SCJP 5.0 Page 517
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:01:36.