开发者

Strange behavior while listing directory contents using Java.io.File

开发者 https://www.devze.com 2023-01-20 14:25 出处:网络
(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 th

(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:\?


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.


e: will list the CURRENT directory in the e: drive

e:\ will list the ROOT directory in the e: drive

Try this:

cd c:\windows\system32

dir c:
dir c:.
dir c:\

"c:" is the same as "c:."


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

0

精彩评论

暂无评论...
验证码 换一张
取 消