File.getName() yes and no?

Discussion in 'Java' started by vbhelpski@yahoo.com, May 2, 2005.

  1. Guest

    Having a hard time understanding this behavior

    File(".")
    File("/")

    Given the File object above, output of ":::" + x.getName() + ":::" is

    :::.:::
    ::::::

    How does this definition of getName() prepare me for
    the above output? If '/' is special in that it is the
    'separator character', then how would one obtain the name
    of the root? getParent() tells me if I have a root name -
    it throws.

    thanks

    getName
    public String getName()Returns the name of the file or directory
    denoted by this abstract pathname. This is just the last name in the
    pathname's name sequence. If the pathname's name sequence is empty,
    then the empty string is returned.

    Returns:
    The name of the file or directory denoted by this abstract pathname, or
    the empty string if this pathname's name sequence is empty
     
    , May 2, 2005
    #1
    1. Advertising

  2. wrote:

    No need to post it twice.

    > Having a hard time understanding this behavior
    >
    > File(".")
    > File("/")
    >
    > Given the File object above, output of ":::" + x.getName() + ":::" is
    >
    > :::.:::
    > ::::::
    >
    > How does this definition of getName() prepare me for
    > the above output? If '/' is special in that it is the
    > 'separator character', then how would one obtain the name
    > of the root? getParent() tells me if I have a root name -
    > it throws.
    >
    > thanks
    >
    > getName
    > public String getName()Returns the name of the file or directory
    > denoted by this abstract pathname. This is just the last name in the
    > pathname's name sequence. If the pathname's name sequence is empty,
    > then the empty string is returned.
    >
    > Returns:
    > The name of the file or directory denoted by this abstract pathname, or
    > the empty string if this pathname's name sequence is empty


    Read the class-level documentation of the File class, particularly the
    part about pathname prefixes. On a system with UNIX filename semantics,
    the string "/" corresponds to an absolute abstract pathname with zero
    names in it. (Hence the unique filesystem root.) The name "."
    corresponds to a relative abstract pathname with one name, ".", in it.
    The behavior you report for File.getName() is thus exactly as
    documented, provided you observed it on a UNIX-like system.

    File.listRoots() gives you a File[] containing the available filesystem
    roots (there may be more than one). You can determine whether a
    particular File represents a filesystem root by leveraging the behavior
    you already saw:

    if (file.getCanonicalFile().getName().equals("")) {
    // File file represents a filesystem root
    }

    That always works and is system-independent, but methods based on
    File.getParent() are not reliable because it is possible to have a File
    with an empty name sequence that is *relative* (e.g. new File("")).
    Such a File does not represent a filesystem root, but its getParent()
    and getParentFile() methods will still return null, just like those of
    Files representing filesystem roots do.

    --
    John Bollinger
     
    John C. Bollinger, May 2, 2005
    #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. Tom
    Replies:
    1
    Views:
    8,386
    Paul Tomblin
    Jun 27, 2003
  2. Rico
    Replies:
    4
    Views:
    427
    Chris Smith
    Dec 1, 2003
  3. Rico
    Replies:
    4
    Views:
    435
    Michael Borgwardt
    Dec 2, 2003
  4. teo
    Replies:
    3
    Views:
    570
  5. Jamie Herre
    Replies:
    1
    Views:
    228
    why the lucky stiff
    Jan 7, 2005
Loading...

Share This Page