?
Solved

File.getName(), root has no name?

Posted on 2005-05-01
10
Medium Priority
?
239 Views
Last Modified: 2012-05-05
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?

 
0
Comment
Question by:hank1
  • 5
  • 5
10 Comments
 
LVL 14

Expert Comment

by:StillUnAware
ID: 13906019
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
 
LVL 1

Author Comment

by:hank1
ID: 13906025
Well, I don't get a null, I get an empty string - so that is
a name of sorts.  Parent nulls.
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 13906028
As an example, this returns null

String root = new File("C:").getParent();
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:hank1
ID: 13906033
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
 
LVL 14

Expert Comment

by:StillUnAware
ID: 13906034
on what platform are You working? and what is your directory structure?
0
 
LVL 14

Accepted Solution

by:
StillUnAware earned 80 total points
ID: 13906043
OK, as You noticed Yourself, Java is removing the first character '/' from a path, so You can always attach one to Your string.
0
 
LVL 1

Author Comment

by:hank1
ID: 13906060
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
 
LVL 1

Author Comment

by:hank1
ID: 13906067
No, it's not always removing it.  Say, have you used this
File before?
0
 
LVL 1

Author Comment

by:hank1
ID: 13906087
U R Right!
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 13906092
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question