5

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?

6
  • 2
    Have you tried File file = new File("C:\\somedir\\report\\"); (note the trailing slashes). No idea if that works but worth a quick build and test. Commented Jan 28, 2010 at 12:55
  • 2
    What kind of OS allows this anyway? I was under the impression that Windows doesn't allow this. Is it possible that your directory has some invisible characters in the name (a space, for example)? Commented Jan 28, 2010 at 13:12
  • I just tried to create a folder named "Folder" and a extension-less file named "Folder" and windows complained about it. I'm running Windows XP SP3 32-bit. Commented Jan 28, 2010 at 13:20
  • @crosvenir: but be careful: just because the UI (explorer.exe) doesn't allow it doesn't mean that it's unsupported. Commented Jan 28, 2010 at 13:24
  • Oh, yeah... Good catch. For instance explorer.exe doesn't allow you to create a file with only an extension as a name (i.e. .cvsignore), but the file system DOES support it. Commented Jan 28, 2010 at 19:04

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yep that's certainly one solution to my problem. But quite inefficient, does anyone know anything better?
@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).
@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 :-)
I'm going to greentick this answer as it was the best anyone came up with!!
3

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?

2 Comments

Yes, that's obvious... But how would you ever get a handle of the actual directory? That's the whole point here.
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.
1

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 ); } } 

1 Comment

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.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.