In windows, jdk1.4.2
Why does:
public static void main(String[] args) {
File f = new File("C:\\com1.txt");
System.out.println(f.exists());
}
return TRUE - when no file of that name is present!
Because (and you should have checked the documentation
yourself, of course !.. ), this line 'new File("C:\\com1.txt");'
CREATES a file !
From the API-docs :
File(String pathname)
Creates a new File instance by converting the given
pathname string into an abstract pathname.
Reread that excerpt. Just like any constructor, that one creates a new
instance of its class (File, in this case). That is different from
creating a file on the filesystem, which it should not do.
The correct answer is: the file DOES exist. Welcome to Windows.
Dating way back to DOS, COM1 (case insensitive) is the name of the first
serial port as exposed by the kernel / command shell. I don't know when
it became the case that you could decorate it with a path and an
extension, but apparently nowadays you can, and it still (appears to)
refer to the serial port. You can even copy a file to c:\com1.txt, and
it will never show up on the filesystem; presumably the OS tries to (or
does) send it out the serial port. The behavior seems to be the same
for any path and extension, but if you change "com1" to something else
("com10", for instance) then you get the expected behavior. Some other
file names you might want to avoid on Windows: com2, com3, com4, lpt1,
lpt2, lpt3, nul, con.
All of this is the behavior of Windows running under Java, not the
behavior of Java itself.
John Bollinger
(e-mail address removed)