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

Discussion in 'Java' started by Bastard, Nov 13, 2003.

  1. Bastard

    Bastard Guest

    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!
    Bastard, Nov 13, 2003
    #1
    1. Advertising

  2. Bastard

    Guest

    >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.
    , Nov 13, 2003
    #2
    1. Advertising

  3. 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.
    Michael Borgwardt, Nov 13, 2003
    #3
  4. Bastard

    Illya Kysil Guest

    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
    Illya Kysil, Nov 13, 2003
    #4
  5. 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
    Joona I Palaste, Nov 13, 2003
    #5
  6. 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
    John C. Bollinger, Nov 13, 2003
    #6
  7. (Bastard) wrote in message news:<>...
    > 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
    Chris Riesbeck, Nov 13, 2003
    #7
  8. Bastard

    Roedy Green Guest

    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.
    Roedy Green, Nov 13, 2003
    #8
  9. Bastard

    Bastard Guest

    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
    Bastard, Nov 16, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lrantisi
    Replies:
    2
    Views:
    12,126
    ashishkhadpe
    Apr 15, 2011
  2. jloyzaga

    write excel file named todays date

    jloyzaga, Jan 18, 2009, in forum: Python
    Replies:
    0
    Views:
    316
    jloyzaga
    Jan 18, 2009
  3. gert

    with open('com1', 'r') as f:

    gert, Apr 2, 2009, in forum: Python
    Replies:
    16
    Views:
    1,071
    Lawrence D'Oliveiro
    Apr 6, 2009
  4. gert
    Replies:
    2
    Views:
    380
  5. Dariush
    Replies:
    4
    Views:
    185
    Tintin
    Nov 19, 2003
Loading...

Share This Page