Hello everyone!

I'm developping a chat application in a client/server context and every client has a shared directory. I want the server to know the directory structure of each shared directory of every client. I'm asking myself if there's an existing class in the Java API that can keep a directory structure on the server side, without containing the entire files.

I was thinkning of the File class, but it seems to work with a "real" file system on a "client side". I'm only interested for the structure of the shared directory (directory names and file names). So, if someone knows the existence of a class that can satisfy my needs, I would like to know. Otherwise, I will have to code a DirectoryTree class.

Recommended Answers

All 4 Replies

You can use the File class wherever the "real" files are, but as soon as you have a File object for a directory that provides the list of files you can use your existing sockets to write/read the object via the socket stream. Once you have a copy of it at the far end you should be able to use its methods to peruse the file list. (But I haven't actually tried this myself!)

> but it seems to work with a "real" file system on a "client side".

I'm not really sure what you mean by a `real' file system; the File class provides an `abstract filesystem independent view' of the path names i.e. your File object does *not* map to a real file on your file system. It's more of an abstraction provided to deal with `file' resources represented using path names.

BTW, I'm personally in favor of having a separate application specific class to represent a File or Directory structure. At first it might end up being just a thin wrapper for the File class but as and when features are added, it would serve as a good abstraction for containing all `File' or `Directory' specific state and behavior of your application.

commented: For every post you write I find it difficult not to give a good rep +9

There's a complete re-work of the File model in the planning for Java 7 - you may find that interesting.
What I meant by "real" file system was simply that the File class relates to actual directory/file structures in an actual mounted file system on the same machine. Or more specifically, to paths within said structure. There's no guarantee that a File object's path will relate to an actual readable or writeable file, but you can assume that the path will be a valid path in a currently mounted file system.
I think this means that the current File class is exactly what you refer to as "specific class to represent a File or Directory structure" - that is all that File is, despite its misleading name. Once again, the discussions around the proposed new file IO classes really help to clarify all this.

Yes, Java 7 indeed seems to bring a lot of interesting things to the table. More confusion FTW!

> What I meant by "real" file system was simply that [...]

My explanation was actually aimed at the OP since I thought he was getting confused between the `File' class and the file-system file. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.