COMP204-07B Assignment 4

Building a disk usage utility

Your task is to implement a simple command-line utility
that recursively scans a directory and all its sub-directories
and reports the total number of bytes needed for every entry
[like a very simplified variant of the Unix du utility].

Entries come in 3 different types:

 plain files: use their file size
 directories: sum the size of all entries (recursing on sub-directories)
 links/aliases: ignore

Your solution should accept two optional command line arguments:

 -s only summarize, i.e. do not list everything, only top-level items

 PATHNAME the directory to start with, if not supplied use the
    current working directory: System.getProperty("user.dir");

Therefore, assuming the name of your class is DiskUsage, the synopsis for your program is:

java DiskUsage [-s] [file|pathName]

The output should list all items sorted by size and flagged as either FILE or DIR. 
Here are a few sample runs:

java DiskUsage /Users/bernhard/cc2/

14 FILE /Users/bernhard/cc2/cmc/ParseIt.java~
26 FILE /Users/bernhard/cc2/cmc/antiPatterns~
56 FILE /Users/bernhard/cc2/META-INF/MANIFEST.MF
56 DIR  /Users/bernhard/cc2/META-INF
56 FILE /Users/bernhard/cc2/sss/x
62 FILE /Users/bernhard/cc2/AUTHORS
129 FILE /Users/bernhard/cc2/weka/classifiers/bayes/README
...
14719826 DIR  /Users/bernhard/cc2/net
35382548 FILE /Users/bernhard/cc2/sss/rcv1-v2.topics.qrels
60806800 DIR  /Users/bernhard/cc2/sss
85712886 DIR  /Users/bernhard/cc2



[current directory is /Users/bernhard/cc2/, which has a sub-dir called test]

java DiskUsage test

988 FILE /Users/bernhard/cc2/test/Files$Item.class
2589 FILE /Users/bernhard/cc2/test/Files.class
2993 FILE /Users/bernhard/cc2/test/Files.java~
3024 FILE /Users/bernhard/cc2/test/Files.java
9594 DIR  /Users/bernhard/cc2/test



[current directory is /Users/bernhard/, which has a sub-dir called cc2]

java DiskUsage -s cc2

56 DIR  /Users/bernhard/cc2/META-INF
62 FILE /Users/bernhard/cc2/AUTHORS
265 FILE /Users/bernhard/cc2/THANKS
991 FILE /Users/bernhard/cc2/NEWS
1280 FILE /Users/bernhard/cc2/TODO
3735 FILE /Users/bernhard/cc2/README
6148 FILE /Users/bernhard/cc2/.DS_Store
9808 DIR  /Users/bernhard/cc2/test
17989 FILE /Users/bernhard/cc2/COPYING
18718 DIR  /Users/bernhard/cc2/gnu
64713 FILE /Users/bernhard/cc2/n.tgz
64890 FILE /Users/bernhard/cc2/sources.zip
78023 DIR  /Users/bernhard/cc2/doc
104177 DIR  /Users/bernhard/cc2/txt
140870 DIR  /Users/bernhard/cc2/ATTIC
176486 DIR  /Users/bernhard/cc2/nci
251841 DIR  /Users/bernhard/cc2/hill
530491 FILE /Users/bernhard/cc2/jode-1.1.2-pre1.jar
858041 DIR  /Users/bernhard/cc2/rcv12
904127 DIR  /Users/bernhard/cc2/jode
1626715 DIR  /Users/bernhard/cc2/cmc
5330796 DIR  /Users/bernhard/cc2/weka
14719826 DIR  /Users/bernhard/cc2/net
60806800 DIR  /Users/bernhard/cc2/sss
85716848 DIR  /Users/bernhard/cc2

What to submit:

One java file containing all you code.
Document your code well.
Use the javadoc @author tag to give your name and ID!

As always, assessment will be based on the following criteria:

  1. Functionality
  2. Reasonable clarity and style of programming.
  3. Reasonable documentation;

Here are a few hints:

  1. the class java.io.File has everything you need.
  2. it does not have an explicit link/alias test, but methods that allow you to detect links/aliases indirectly ...
  3. be careful about relative pathnames, there is a method getAbsoluteFile() in Class File to help you circumvent problems with relative pathnames
  4. Sorting is easy, remember Collections.sort, or Arrays.sort