Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

File.getName() yes and no?

Thread view: 
vbhelpski@yahoo.com - 02 May 2005 13:56 GMT
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
John C. Bollinger - 02 May 2005 18:06 GMT
No need to post it twice.

> Having a hard time understanding this behavior
>
[quoted text clipped - 22 lines]
> 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.

Signature

John Bollinger
jobollin@indiana.edu



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.