File.getName() yes and no?

V

vbhelpski

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
 
J

John C. Bollinger

(e-mail address removed) 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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top