aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes i/o Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "i/o" Watch "i/o" New topic
Author

i/o

devi nachimuthu
Greenhorn

Joined: Jan 07, 2003
Posts: 10
What will be the output. Consider a directory structure like this (NT or 95)
C:\JAVA\12345.msg --File
import java.io.*;
public class IO {
public static void main(String [] args) {
File f1 = new File("\\12345.msg");
System.out.println(f1.getPath());
System.out.println(f1.getParent());
System.out.println(f1.iaAbsolute());
System.out.println(f1.getName());
System.out.println(f1.exists());
System.out.println(f1.isFile());
}
}
The given answer for this is
\12345.msg
\
true
12345.msg
false
false
Why it is giving the result false for f1.exists() and f1.isFile(). It has given that 12345.msg is file and is in c:\java\12345.msg--file
Can somebody please explain....?
Thanks
Sachin Kombrabail
Greenhorn

Joined: Aug 28, 2000
Posts: 14

First of all when you created the file object using new File("\\12345.msg"), it will be looking for a file in the C:\ directory and not C:\JAVA directory. Methods like getPath, getParent, iaAbsolute, getName will be using the filename to give you the result. It is only when you come to exists() that it really tries to find out whether the file is there or not. Since you are looking in the wrong directory it will not find the file there and will give you a false. Same is true with isFile().
Why don't you change your new File() to the correct path of the file and see how it works.
 
 
subject: i/o
 
Threads others viewed
IO question
getPath()
File Object, Poddar Exam Q: 41
whether getabsolutepath() and getcanonicalpath() are in scjp exam
doubt in File methods
IntelliJ Java IDE