views:

127

answers:

4

I'm running Windows and I'm trying to refer to a directory. My function starts off like this:

File file = new File("C:\\somedir\\report");
if (!file.exists()) {
  file.mkdirs();
}
doStuffWith(file);

I got a NullPointerException within the doStuffWith function, when I tried to call listFiles. Well I looked in C:\somedir and what did I find - there is a file called "report" with no extension, and also a directory called "report"! What seemed to happen was that the file object was referring to the report file rather than the directory. How do I make sure that I am referring to the directory and not the file?

+3  A: 

i think there is a isDirectory() method that will tell you if it is a directory

--EDIt

that's what I get for being up so early. I ran your code locally and it works fine for me. Was able to create new files, read directory contents, etc. What else are you trying to do?

Yes, that's obvious... But how would you ever get a handle of the **actual directory**? That's the whole point here.
BalusC
I agree with BalusC above, this doesn't answer my question, sorry...
Kidburla
Again i'm afraid you've misread my question. The code will work in most cases, but I was interested in the specific case where there is an extensionless file called "report" in the "somedir" directory.
Kidburla
+3  A: 

one way to go about is to pass the file object corresponding to "C:\somedir" to the method and inside the method, do a listFiles() and walk through the contents, each time checking for file name and if it is "report", do a isDirectory(). proceed with actual processing when this returns true.

Aadith
Yep that's certainly one solution to my problem. But quite inefficient, does anyone know anything better?
Kidburla
@Kidburla: did this actually work? If it does, then you could look at the two File objects and see the difference (in the worst case you might want to look with reflection).
Joachim Sauer
@Kiburla I know its not the best approach...this just happened to be the first thing that occured to me when i looked at the problem..and thought it worthy of sharing :-)
Aadith
I'm going to greentick this answer as it was the best anyone came up with!!
Kidburla
+1  A: 

I don't understand the problem this works fine for me:

public class MkDir {
    static void doStuff(File dir) {
        if ( dir.isDirectory() ) {
            File[] listFiles = dir.listFiles();
            for ( File f : listFiles ) {
                System.out.println( f.getName() );
            }
        }
    }

    public static void main(String[] args) {
        File file = new File( "C:\\dev\\rep2\\rep" );
        if ( !file.exists() ) {
            file.mkdirs();
        }
        doStuff( file );
    }
}
stacker
I'm afraid you've misread my question. The code will work in most cases, but I was interested in the specific case where there is an extensionless file called "report" in the "somedir" directory.
Kidburla
A: 

Check if your file system has had case sensitivity enabled (HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\ dword:ObCaseInsensitive in the registry.)

If so, you may be getting bitten by a case-related issue. One way to check:

String someName = "./nameNotUsedYet";
boolean first = new File(someName).mkdirs();
boolean second = new File(someName.toUpperCase()).mkdirs();
System.out.println("first = " + first + ", second = " + second);

If both mkdirs() calls succeeded, you know you have a case related complication. If so, ensure that you get the case for "C:\somedir\report" exactly right.

Dilum Ranatunga