File.getName(), root has no name?

I'm drilling into the drive and start so...

   try {

      File root = new File(directoryName);

      if (!root.isDirectory() ||
           root == null) {
        throw new RuntimeException("Need a directory, not file");
      }

      me.drillIt(root, me);


Here's a little part of the function called from above.


  private void drillIt(File dir, Drill me) {

    try {
      String tmp01 = new String(dir.getName());  // empty string?
      String tmp02 = new String(dir.getParent()); //null
    } catch(Exception e) {
      System.out.println("Something didn't work: " + e.getMessage());
    }



Was most suprised to find that the directory '/', has no
name.  Trying to get its name get's u a null.  Should this
work like this?

 
LVL 1
hank1Asked:
Who is Participating?
 
StillUnAwareCommented:
OK, as You noticed Yourself, Java is removing the first character '/' from a path, so You can always attach one to Your string.
0
 
StillUnAwareCommented:
Yes, it is the right behaviuor. Read the documentation:

Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory. If the name sequence is empty then the pathname does not name a parent directory.

It means, if you are already in the root, and trying to get the root from the root itself, You'll get null identifying, the current directory is already THE root.
0
 
hank1Author Commented:
Well, I don't get a null, I get an empty string - so that is
a name of sorts.  Parent nulls.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
StillUnAwareCommented:
As an example, this returns null

String root = new File("C:").getParent();
0
 
hank1Author Commented:
And if I start with , say, /usr, the name of the dir
will be 'usr'.  Same question.  The name of root is /
and not empty string - in my estimation anyways.
Oh well.  Thanks.  And thanks for the read the API tip too.
0
 
StillUnAwareCommented:
on what platform are You working? and what is your directory structure?
0
 
hank1Author Commented:
Oh, here's something.  W2K

While File has no problem with / or \ as a path delimiter,
if you drill from the root with

java Drill /
 
you get no directory name.  However, if you start with

java Drill \

you do get a directory name.  I think this is strange.
0
 
hank1Author Commented:
No, it's not always removing it.  Say, have you used this
File before?
0
 
hank1Author Commented:
U R Right!
0
 
StillUnAwareCommented:
Well, and again I suggest You read the documentation of File, from the begining. If it won't help You, I doubt I will.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.