File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chapter 6 from K&B question 12

 
kirba devi
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kirba devi
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
}
}
}
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic