Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Why is isDirectory true for File("c:")?

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Why is isDirectory true for File("c:")?

    Hello!

    I've create a small program, and when I create a File object like

    File f = new File("c:")

    and then call the method isDirectory, it returns true. Why is that?

    The program is suppose to show all files in that directory and works fine except when Im using the "c:". It is not accessing "c:\", not the home path, but the directory the program is executing from. I really don't understand.

    Hank


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why is isDirectory true for File("c:")?

    This is an interesting question. I'll try to expand on your question (questions should always come with an SSCCE whenever possible)...

    Take this code for example:

    import java.io.File;
     
    public class Main{
     
    	 public static void main(String[] args){
    	        File f = new File("C:\\");
    	        System.out.println("C:\\ is directory: " + f.isDirectory());
    	        System.out.println("C:\\ exists: " + f.exists());
    	        System.out.println("C:\\ absolute path: " + f.getAbsolutePath());
    	        System.out.println();
     
    	        f = new File("C:");
    	        System.out.println("C: is directory: " + f.isDirectory());
    	        System.out.println("C: exists: " + f.exists());
    	        System.out.println("C: absolute path: " + f.getAbsolutePath());
    	        System.out.println();
     
    	        f = new File("X:");
    	        System.out.println("X: is directory: " + f.isDirectory());
    	        System.out.println("X: exists: " + f.exists());
    	        System.out.println("X: absolute path: " + f.getAbsolutePath());
    	        System.out.println();
    	    }
     
    }

    (Note that I do have a C: drive, I do not have an X: drive)
    This results in this output:

    C:\ is directory: true
    C:\ exists: true
    C:\ absolute path: C:\

    C: is directory: true
    C: exists: true
    C: absolute path: C:\Users\kworkman\Desktop\Tests

    X: is directory: false
    X: exists: false
    X: absolute path: X:


    So it seems to handle the C:\ correctly, but C: by itself results in strange output for a directory that doesn't actually exist. This is made stranger because it seems to handle the X: case correctly.

    I'm not sure what's going on. I'll be curious to see what the smarter people come up with.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Why is isDirectory true for File("c:")?

    Yes. It certainly is very strange and, as you say, it seems that this only occurs with "c:".

    It will be interesting to find out what's going on.

    Hank

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Why is isDirectory true for File("c:")?

    To come up with another case:
    C:\ is directory: true
    C:\ exists: true
    C:\ absolute path: C:\

    C: is directory: true
    C: exists: true
    C: absolute path: C:\

    X: is directory: false
    X: exists: false
    X: absolute path: X:

    JDK 7, win7 pro 64bit

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why is isDirectory true for File("c:")?

    Quote Originally Posted by PhHein View Post
    JDK 7, win7 pro 64bit
    Stranger still, that's exactly what I'm running, yet we get different output?

    (I'm using Java 7 update 51)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Why is isDirectory true for File("c:")?

    I believe the culprit is this code from the WinNTFileSystem class:
    public String resolve(File f) {
            String path = f.getPath();
            int pl = f.getPrefixLength();
            if ((pl == 2) && (path.charAt(0) == slash))
                return path;                        /* UNC */
            if (pl == 3)
                return path;                        /* Absolute local */
            if (pl == 0)
                return getUserPath() + slashify(path); /* Completely relative */
            if (pl == 1) {                          /* Drive-relative */
                String up = getUserPath();
                String ud = getDrive(up);
                if (ud != null) return ud + path;
                return up + path;                   /* User dir is a UNC path */
            }
            if (pl == 2) {                          /* Directory-relative */
                String up = getUserPath();
                String ud = getDrive(up);
                if ((ud != null) && path.startsWith(ud))
                    return up + slashify(path.substring(2));
                char drive = path.charAt(0);
                String dir = getDriveDirectory(drive);
                String np;
                if (dir != null) {
                    /* When resolving a directory-relative path that refers to a
                       drive other than the current drive, insist that the caller
                       have read permission on the result */
                    String p = drive + (':' + dir + slashify(path.substring(2)));
                    SecurityManager security = System.getSecurityManager();
                    try {
                        if (security != null) security.checkRead(p);
                    } catch (SecurityException x) {
                        /* Don't disclose the drive's directory in the exception */
                        throw new SecurityException("Cannot resolve path " + path);
                    }
                    return p;
                }
                return drive + ":" + slashify(path.substring(2)); /* fake it */
            }
            throw new InternalError("Unresolvable path: " + path);
        }

    Both "C:" and "X:" have pl==2.
    For "C:", this return statement is used:
    if ((ud != null) && path.startsWith(ud))
                    return up + slashify(path.substring(2));
    For "X:", String dir = getDriveDirectory(drive); returns null, so the if statement is not entered, and the "fake it" return statement is used:
    return drive + ":" + slashify(path.substring(2)); /* fake it */
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Why is isDirectory true for File("c:")?

    java version "1.7.0_05"
    Java(TM) SE Runtime Environment (build 1.7.0_05-b06)
    Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)
    On eclipse Indigo SR2

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why is isDirectory true for File("c:")?

    And I'm on eclipse Juno SR1

    This doesn't guarantee this is what eclipse is using, but in my case, they are the same:

    C:\Users\kworkman>java -version
    java version "1.7.0_51"
    Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

    I think Aussie is on the right track, but I'm still not sure why we get different output.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Why is isDirectory true for File("c:")?

    Quote Originally Posted by PhHein View Post
    To come up with another case:
    C:\ is directory: true
    C:\ exists: true
    C:\ absolute path: C:\

    C: is directory: true
    C: exists: true
    C: absolute path: C:\

    X: is directory: false
    X: exists: false
    X: absolute path: X:

    JDK 7, win7 pro 64bit
    In the middle example, from what path are you running the program?

  10. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Why is isDirectory true for File("c:")?

    Maybe your FileSystem objects are different. I'm not sure how you can get the instance of that. It seems to be protected.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  11. #11
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Why is isDirectory true for File("c:")?

    Quote Originally Posted by aussiemcgr View Post
    Maybe your FileSystem objects are different. I'm not sure how you can get the instance of that. It seems to be protected.
    For me, "c:" is always the path of the the executing program. When I move the executing file, then the path och File("c:") is changing to that directory. That is why I think that PhHein is executing his program from C:\ in the above example.

Similar Threads

  1. Understanding layers of system logic. Clarity for what "SDK" & "JDK" mean
    By MilkWetGhost in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2013, 12:25 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM