Todays strange bug? File.exists(com1.txt)

B

Bastard

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!
 
J

johnR

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.
 
M

Michael Borgwardt

Because (and you should have checked the documentation
yourself, of course !.. ), this line 'new File("C:\\com1.txt");'
CREATES a file !

No, it doesn't. Read the API doc again, carefully this time.
 
I

Illya Kysil

Hello, Bastard!
You wrote on 13 Nov 2003 07:43:00 -0800:

B> In windows, jdk1.4.2

B> Why does:

B> public static void main(String[] args) {
B> File f = new File("C:\\com1.txt");
B> System.out.println(f.exists());
B> }

B> return TRUE - when no file of that name is present!
Have you forgotten an old COM1/COM2/COM3/etc. from DOS times?
Those names are still reserved...

Regards
Illya Kysil, software developer
Delphi/C/C++/C#/Java/Forth/Assembler
If it is NOT SOURCE, it is NOT SOFTWARE. (C) NASA
 
J

Joona I Palaste

No, it doesn't. Read the API doc again, carefully this time.

I was figuring there'd be little point to the exists() method if the
File() constructor created a new file...
 
J

John C. Bollinger

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)
 
C

Chris Riesbeck

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!

com1 through com9 are reserved names in Windows -- even NT-based
systems -- for com ports, even with extensions like .txt
 
B

Bastard

Roedy Green said:
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

See http://mindprod.com/file.html

You run into the same problem with null and lpt?.*

Thanks to all that pointed to that solution. I was was able to guess
that the problem lay in windows confusing com1.txt with them com:
port, but really needed the confirmation.

Im a happy user of Java Glossary, but overlooked the exists() example
:( - Keep up the good work Roedy!

/B
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top