• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

i/o

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic