Search

Sunday, June 2, 2013

Java file organization - part 2

So last post I showed how I built a few quick file utilities in Java. If you will recall, I have a very large music collection I copy to (currently) 8 usb thumb drives for use in my car. I do not want bands broken up between disks, and I want to avoid copying files that my car stereo does not read. So I wrote a few Java utilities to help me automate this task as my music collection grows.

This post I am going to show two more utilities, and then I'll show how I use these methods to build my disks. We will need a method to copy files and a method to delete files (to clean out the old files when I rebuild the disks). I'll start with the remove method because it is the smallest.

Basically we give it a folder, and it will remove all children. Our general strategy is to first collect all files and simply remove them. Next we iterate through toCheck and collect all the remaining directories and store them for removal as we continue to check them. We also add the current directory to the list to remove. This is similar to the logic we used in the last post to build our folder listing. Finally, we reverse toRemove so that we are removing the deepest nested directories first, and we iterate again to remove them all. This should all be rather straightforward.

The next method is a bit more complicated, but not too bad. We're using nio channels to perform the actual copying of the contents.

This method will return a long representing the size of the copied file (0 if the copy fails). The input is the file which is being moved, a disk number (used to name the target directory), a new root path, and the old root path. These paths are used to determine where to copy the file to. For instance, I have my music stored as /usr/home/me/music/<arrtist>/<album>/, so the oldPath would be /usr/home/me/music/. I want to store these disks to /mnt/<disknum>, so the newPath would be /mnt/. Everything else about the path would be preserved, so if our old file was /use/home/me/music/coolband/bestalbum/bestsong.mp3, it would be copied to /mnt/01/coolband/bestalbum/bestsong.mp3 (assuming this is destine for the first disk).

The meat of this method starts out by building the name for the new path, creating any necessary directories which contain this file, and creating the actual file. From there we connect IO streams to the source and destination files, and retrieve their channels for reading and writing. Once we have the size stored, the actual instruction to transfer the file is basic, we call the transferTo method on the output target, and give it the start and end position as well as the input source channel. The rest of the method is exception handling in case something goes wrong, and we finish by returning the size of the transfer. It is interesting to note that the file copy itself is only one line, and even with setting up the IO, it's still less than 10. Half if not more of the coding work involved is in admin work like generating path names and creating parent directories.

We start out by defining some limitations imposed by the hardware involved. The maxSize is a limit of my usb thumb drives (approx 64*10^9), and the maxDir and maxFiles is a limitation of my player. The rootDir is where we will build our disks from, our master list. The newLocation is where we will copy our files to build our disks.

For the meat of our method we define a few numeric types to track the size, number of files, and number of directories in each disk; we define a diskList to hold each listing of files which will make up each disk. Note that this is a nested list, meaning it is a linked list of linked lists. We have created one linked list that stores other linked lists of files, and we will add them to it as we assemble them. Next we create our first file list (representing a disk) and add it to the disk list. The last step before we start to assemble our disks is to gather a list of all the artists, which each have a separate directory under the rootDir, we call this list the headList, and we sort so that the disks are split up alphabetically by artist/band.

Once this is all setup we simply iterate through each artist/band and collect a list of their files, directories, and a total of the size. If they overflow our current disk we start on a new disk, otherwise, we add them to our current file list, and move on to the next. Once this is all done and every disks file list is assembled we remove everything from our newLocation (this assumes that you have old disks that were built that will be superseded by the ones about to be copied), and we copy all the files to their associated disks. Note that this last for loop would be faster if it iterated through diskList instead of using pop() because pop() incurs the overhead of un-linking a list node, though compared to the time that the copy itself takes, this is negligible...

There are lots of other things you an do to automate maintenance on large digital collections like this. Do you have any ideas?

Subscribe To

Followers

Disclaimer:

My intent is purely to inform and educate. As such, names and dates may be changed. Specifically, if I believe information posted could be discerned by the parties involved, I will purposefully withhold the post till I believe the information is either not relevant to the situation any longer, or that information is of such an important nature to the public that I feel it would be irresponsible to withhold it.

Copyright 2011, you may not reuse my work without permission. Awesome Inc. theme. Powered by Blogger.