Files and Directories in the JDK
Join the DZone community and get the full member experience.Join For Free
in java, java.io.file is one of the more frequently used low-level api objects. it also happens to be lacking in some basic functionality that we’ve all needed at some point, doesn’t provide different representations/api for files and directories and doesn’t throw fine-grained exceptions to differentiate between different types of error conditions (i.e., file already exists, directory not empty, invalid path, etc.)
at carfey, we’ve been using our own file and directory classes for years and have been enhancing their utility over time by adding new functionality as the need arises – file.movetofile(file newfile), file.writefromstream(inputstream is), directory.listfilesrecursively(), directory.emptydirectory, etc.
probably the only real pain you’d encounter using an alternate representation of java.io.file would be in interacting with other libraries that would need access to java.io.file objects. that is easily accommodated by exposing a getjavafile() method on each class.
free for use
attached you can find our classes and test code available under the mit licence. it does depend on our open-source date library which we spoke about here . if you’d rather skip the extra library, you can drop the getlastmodifieddate method from the file class.
here are some of the highlights.
first, we use different classes to represent file and directory. rather than having to invoke isfile() and isdirectory(), we have the type to guide us. our directory class has some public constants, such as path_separator which is equivalent to java.io.file.pathseparator but uses standard constant naming convention. also temp_dir, which provides easy access to the temporary directory loaded using the system property java.io.tmpdir. we get typed exceptions such as directorynotcreatedexception, invaliddirectoryexception, directorynotdeletedexception with descriptive messages when attempting to construct a directory reference or physically create/delete a directory but encountering problems. we can listfiles() or even listfilesrecursively() on a directory. want to completely empty a directory of files and subdirectories? use emptydirectory().
our file class similarly gives us typed exceptions (invalidfileexception, fileioexception, filenotdeletedexception, filenotdeletedexception) with descriptive messages on construction, create or delete of physical files but encountering problems. we have very convenient move and write methods: movetofile(file movefile), writefromstream(inputstream is), write(string contents). more convenience methods for getting a fileinputstream or fileoutputstream for the given file. even a getdirectory() that will return one of our directory objects representing the containing directory.
also in the bundle is our own ioutil class that provides some necessary functions for our file and directory classes. you happen to get some of its own helpful functionality including
public static final byte getbytes(inputstream is) public static long copystream(inputstream src, outputstream dest, int buffersize, boolean flusheachread, streamlistener... listeners) public static inputstream streamfromreader(reader reader) public static string streamasstring(inputstream is, string encoding) public static <t extends serializable> t clonethroughserialize(t t) public static object deserialize(inputstream is) public static void serializetooutputstream(serializable ser, outputstream os)
if you’re an eclipse user, this brings up an interesting issue with resolving imports. if you add carfey file and directory to your project, you might want to avoid the annoyance of having to select the type of file you want to use. eclipse allows you to customize how imports are done. if you’ve never done this before, check it out. you can use with other common names such as list, util, stringutil, etc.
now when you “organize imports” with ctrl+shift+o, you won’t be prompted to choose which file you want.
happy coding and if you find any bugs or have questions, leave a comment here.
Opinions expressed by DZone contributors are their own.