Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > File.getName() yes and no?

Reply
Thread Tools

File.getName() yes and no?

 
 
vbhelpski@yahoo.com
Guest
Posts: n/a
 
      05-02-2005
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

 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      05-02-2005
http://www.velocityreviews.com/forums/(E-Mail 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.

--
John Bollinger
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Yes means yes Bucky Breeder Computer Support 0 05-19-2009 01:37 PM
yes minister and yes prime minister wanted music_mania DVD Video 11 12-11-2006 07:32 PM
[IE: Yes Opera:Yes Mozilla:No] : Error on Postback and Validation teo ASP .Net 3 11-11-2006 04:53 AM
Kernel.y and yes,yes,yes not least surprise Jamie Herre Ruby 1 01-07-2005 07:33 PM
yes yes Kevin Walsh Computer Support 1 08-30-2004 12:55 AM



Advertisments