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
posted
0
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.