File getName method behaviour on different OS!

Discussion in 'Java' started by Tom, Jun 27, 2003.

  1. Tom

    Tom Guest

    I try this code:
    File file = new File(args[0]);
    String filename = file.getName();
    System.out.println("file name : " + filename);

    I run it on windows with these arguments:
    /opt/test/test1.txt
    c:\windows\test2.txt

    I got this:
    file name : test1.txt
    file name : test2.txt

    If I run it on Linux I get this:
    file name : test1.txt
    file name : c:\windows\test2.txt

    Why I don't get the same thing? I know it does not make sense to write
    down a path that isn't in the system format. I will use an URL format
    to get the real nameā€¦

    Tom
    Tom, Jun 27, 2003
    #1
    1. Advertising

  2. Tom

    Paul Tomblin Guest

    In a previous article, (Tom) said:
    >I try this code:
    > File file = new File(args[0]);
    > String filename = file.getName();
    > System.out.println("file name : " + filename);
    >
    >I run it on windows with these arguments:
    >/opt/test/test1.txt
    >c:\windows\test2.txt
    >
    >I got this:
    >file name : test1.txt
    >file name : test2.txt
    >
    >If I run it on Linux I get this:
    >file name : test1.txt
    >file name : c:\windows\test2.txt
    >
    >Why I don't get the same thing? I know it does not make sense to write


    Because on Windows, the forward slash and the back slash both work as path
    separators, so "/opt/test/test1.txt" would be a file test1.txt in the path
    \opt\test on the current drive. On the other hand, Linux allows
    backslashes and colons in file names, so "c:\windows\test2.txt" would
    indicate a file called "c:\windows\test2.txt" in the current directory.

    Here is a part of an "ls" in my home directory:

    cfs.zip crichton_faq dft-v330img.bin
    cleanfeed-20010715.tgz c:\windows\test2.txt dickhead
    clubtop5 datasources disk



    --
    Paul Tomblin <>, not speaking for anybody
    "The question of whether a computer can think is no more
    interesting than the question of whether a submarine can swim ."
    E. W. Dijkstra
    Paul Tomblin, Jun 27, 2003
    #2
    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. Rico
    Replies:
    4
    Views:
    408
    Chris Smith
    Dec 1, 2003
  2. Rico
    Replies:
    4
    Views:
    426
    Michael Borgwardt
    Dec 2, 2003
  3. Robert Mark Bram

    class.getName()

    Robert Mark Bram, Jun 17, 2004, in forum: Java
    Replies:
    8
    Views:
    4,095
    Jeff Robertson
    Jun 18, 2004
  4. Replies:
    1
    Views:
    454
    John C. Bollinger
    May 2, 2005
  5. WT
    Replies:
    6
    Views:
    935
    Walter Wang [MSFT]
    May 18, 2006
Loading...

Share This Page