Lolmewn
Lolmewn

Reputation: 1521

Java List array showing non existing folders.. Why?

I am in the process of making a program, which allows you to view your file system. I was testing it, and ran into a problem: It was saying a directory called "Documents and Settings" was on my C:\ drive, while it wasn't there.

This is how I get my file array:

File f = new File(path); //path being a path sent by the client, for example C:\
            if(f.isFile()){
                //TODO start downloading it.
                out.println("ERR: no dir!");
                return;
            }
            Server.log.log("System path requested: " + f.getAbsolutePath());
            File[] files = f.listFiles();
            for(int i = 0; i < files.length; i++){
                File found = files[i];
                if(!found.exists()){
                    continue;
                }
                if(found.isDirectory()){
                    out.println("dir:" + found.getName());
                }else{
                    out.println(found.getName());
                }
                System.out.println("Printed " + found.getName());
            }
            out.println("ENDOFLIST"); //Notify the client it has to stop receiving data

For some reason, this outputs quite a lot of directories that I can't seem to find, even with the "Show hidden folders" option on.

When trying to access these directories, it tries to read the contents of the directory, but since the directory doesn't exist it throws an exception, causing no data to get sent over sockets and my client freezing.

My question is: Is there a way to either check if the file/directory REALLY exists? Note, if you look at my code block, if the file/dir doesn't exist it already continues instead of writing it to the socket.

I've given it a google, but no matches were found. Also, I've given the search function a go, but it didn't come up with anything similar.

Upvotes: 1

Views: 424

Answers (2)

Jan Henke
Jan Henke

Reputation: 875

I suggest to use the new Fil I/O API introduced by Java 7, it features greatly improved support of the features a specific file system offers. It also offers the possibility to use walk the file tree.

Have a look at the FileVisitor http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html that will greatly help you.

Upvotes: 1

SLaks
SLaks

Reputation: 887547

These are hidden system folders.
They do exist. Really.

You get exceptions because a lot of them don't have read access.

Upvotes: 3

Related Questions