Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Todays strange bug? File.exists(com1.txt)

Reply
Thread Tools

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

 
 
Bastard
Guest
Posts: n/a
 
      11-13-2003
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!
 
Reply With Quote
 
 
 
 
johnR@notmail.com
Guest
Posts: n/a
 
      11-13-2003
>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.
 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      11-13-2003
wrote:
> 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.

 
Reply With Quote
 
Illya Kysil
Guest
Posts: n/a
 
      11-13-2003
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


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      11-13-2003
Michael Borgwardt <> scribbled the following:
> wrote:
>> 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 was figuring there'd be little point to the exists() method if the
File() constructor created a new file...

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am looking for myself. Have you seen me somewhere?"
- Anon
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      11-13-2003
wrote:

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


 
Reply With Quote
 
Chris Riesbeck
Guest
Posts: n/a
 
      11-13-2003
(Bastard) wrote in message news:<. com>...
> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-13-2003
On 13 Nov 2003 07:43:00 -0800, (Bastard) wrote or
quoted :

> 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?.*



--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Bastard
Guest
Posts: n/a
 
      11-16-2003
Roedy Green <> wrote in message news:<>. ..
> On 13 Nov 2003 07:43:00 -0800, (Bastard) wrote or
> quoted :
>
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What MCSE in todays world makes more sense. Kal MCSE 1 03-25-2007 07:49 PM
Todays date in range validator? Tarun Mistry ASP .Net 2 02-22-2006 09:33 AM
Re: Todays links Mark McIntyre C Programming 5 01-26-2006 12:32 AM
Todays Experience With 42nd Street Photo..NYC rHooD Digital Photography 29 08-28-2004 12:25 AM
using getdate() how can i get todays date in mm/dd/yy format? any sug please kris C++ 1 12-10-2003 01:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57