Link to home
Start Free TrialLog in
Avatar of hank1
hank1

asked on

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?

 
Avatar of StillUnAware
StillUnAware
Flag of Lithuania image

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.
Avatar of hank1
hank1

ASKER

Well, I don't get a null, I get an empty string - so that is
a name of sorts.  Parent nulls.
As an example, this returns null

String root = new File("C:").getParent();
Avatar of hank1

ASKER

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.
on what platform are You working? and what is your directory structure?
ASKER CERTIFIED SOLUTION
Avatar of StillUnAware
StillUnAware
Flag of Lithuania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hank1

ASKER

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.
Avatar of hank1

ASKER

No, it's not always removing it.  Say, have you used this
File before?
Avatar of hank1

ASKER

U R Right!
Well, and again I suggest You read the documentation of File, from the begining. If it won't help You, I doubt I will.