Extending the File class to provide user a consistent way on filesystems over network channels transparently
I'm implementing a Java-based system which needs accessing both the native and networked filesystems. Using the Java File class is easy and straight forward in the native case, and I'd like to keep this consistency to user even the filesystem is connected over the network.
e.g. File folder = new File("some_folder"); File[] files = folder.listFiles();
if the instance "folder" is network-attached, the folder.listFiles()
will trigger the related network transaction to retrieve the remote file structure.
The problems include, is it possible to extending the File class with the same name? If a new given name for the subClass is needed, e.g. NetedFile
, how to deal with the return type in the methods like File[] listFiles()
while I don't want to wrap the element from File
to NetedF开发者_运维技巧ile
one-by-one.
Please kindly advise.
Thanks & regards, William Choi
The good news is that if you mount a remote file system on your machine using NFS, SMB and other protocols, then File
objects for files or directories on the remote FS should just work. No extra coding is required.
The bad news is that this doesn't work for remote file systems that you access using FTP or HTTP. Also, the remote filesystem mounting and access must be implemented at the operating system level.
EDIT
If you are trying to treat a regular HTTP server as a file system, you've got a number of problems, starting with the problem that the standard HTTP protocol does not make any distinction between files or directories. They are all "resources".
If you are talking about a WebDAV enabled HTTP server, there are apparently drivers around that allow you to mount a remote WebDAV server as a file system; e.g. do a Google search for "webdav mount windows" or "webdav mount linux".
If you have to do all of this in Java (e.g. if the proprietary "channel" is a Java class), I don't know how you would go about doing it.
精彩评论