tags:

views:

35

answers:

2

Hi,

(background)
I'm using Java.io.File to do some windows directory searching. Basically the user inputs a directory path and I validate the path with file.isDirectory(). If the path is invalid I ask the user to re-enter a correct path. Once I get a valid directory I proceed with other File operations.

(problem)
Now the problem occurs when the user enters the root directory. For example, e:. In this case file.list() provides the following output [.classpath, .project, .settings, bin, src].
As you can see this does not include any folders.

However if he enters e:\ then file.list() fetches the existing directories also [$RECYCLE.BIN, <some directories>, <some files>, RECYCLER, System Volume Information]

This time we don't have classpath, project etc. yet both e: and e:\ are considered valid directories.

1) Can someone explain this strange behavior?

2) Also, to avoid this problem is there a better method than adding a dirty manual check for x: and converting it into x:\?

+3  A: 

I expect something is interpreting "e:" as the current directory of the volume e: (which may not be the root). Raymond Chen recently covered the history of this sort of thing.

McDowell
+1  A: 

First of all, .settings, bin and src ARE folders.
Secondly, do you actually have those files (.classpath, .project, etc.) in drive e: ?

My guess is, java doesn't parse "e:" correctly, and file.list() gives you the list of files under the jvm current folder, which happens to be the folder where your eclipse project files reside (All those files AND folders that you mentioned belong to an eclipse project).

Try opening a command-line window. On my machine it opens at c:\Windows\System32. Then type "cd c:" and see what happens ...

Yoni
I checked e drive and those folders aren't present. Also, I tried opening the command prompt. For me it opens at c:\users\<my windows account username>\ . It remains the same after I type "cd c:". I'm using windows 7. (You're using windows xp probably).
Mugen
.classpath, .project, .settings, bin, src sound like files in an eclipse project.
Skip Head
`CD C:` doesn't do anything because `C:` is not a directory. It is a drive. From the cmd.exe help: _Type CD drive: to display the current directory in the specified drive. Type CD without parameters to display the current drive and directory._
McDowell
@McDowell, thanks for the clarification. In any case, I was using cd just to illustrate a point. In the end it is about Java's current folder
Yoni
@Skip Yep. It is an eclipse project. However, I don't think that might be directly related to this thing. The output is even achieved running it from basic command prompt. Its kinda annoying that `File` accepts `c:` as a directory even though it's a drive. I was just thinking whether there is something better than manually adding a check to see user didn't enter this.
Mugen