Results 1 to 3 of 3
  1. #1
    vicky08 is offline Member
    Join Date
    Mar 2008
    Location
    wales
    Posts
    2
    Rep Power
    0

    Default newbie needs help...

    Hi everyone,

    I'm a student currently studying IT and now started java. I have been thrown into the deep end and given some java questions to do over the easter period and seem to be struggling. At the moment there is nobody in university to speak to, so thought you guys would be the best solution and was told to use any resources available.

    This is what my lecturer wrote word for word and I don't fully understand what it means? Any help would be greatly appreciated...

    "Complete these classes to provide a command line application Diskusage which outputs to standard output a list of the contents (both files and directories) of a specified root directory and all its subdirectories. Each line of output should either the file size in bytes, or the total size of all files within the directory"



    This is the first command 'FileUtilities' in the question:

    /* add required import statements */

    public class FileUtilities {

    /* complete method declaration */ diskUsage( File f ) {
    /* complete method */
    }

    /* Add further methods? */

    }

    This is the second 'DiskUsage' command:

    public class DiskUsage {
    public static void main( String[] args ) {
    /* error checking? */
    /* set type */ size = FileUtilities.diskUsage( args[0] );
    System.out.println( "Total size of " + args[0]
    + " is " + size );
    }
    }


    Thanks,

    Vicky.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.io.File;
    
    public class DiskUsage {
        public static void main( String[] args ) {
            //args = new String[]{ "." };  // current directory
            if(args.length == 0) {
                System.out.println("Usage: specify a path/folder_or_file");
                System.exit(1);
            }
            File file = new File(args[0]);
            if(!file.exists()) {
                System.out.println("File for " + args[0] + " does not exist");
                System.exit(1);
            }
            long size = FileUtilities.diskUsage( file );
            System.out.println( "Total size of " + file.getPath() +
                                " is " + size );
        }
    }
    
    class FileUtilities {
        static long totalSize;
    
        public static long diskUsage( File f ) {
            if(!f.isDirectory()) {
                return f.length();
            } else {
                totalSize = 0;
                traverseFiles(f);
                return totalSize;
            }
        }
    
        private static void traverseFiles(File file) {
            File[] files = file.listFiles();
            for(int i = 0; i < files.length; i++) {
                if(files[i].isDirectory()) {
                    System.out.println("---- folder " +
                                        files[i].getName() +
                                       " ----");
                    traverseFiles(files[i]);
                } else {
                    totalSize += files[i].length();
                    System.out.println(files[i].getName() +
                                       "  " + files[i].length());
                }
            }
        }
    }

  3. #3
    vicky08 is offline Member
    Join Date
    Mar 2008
    Location
    wales
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Newbie
    By CSnoob87 in forum Introductions
    Replies: 2
    Last Post: 02-18-2008, 08:49 AM
  2. Newbie reporting
    By nelsaez in forum Introductions
    Replies: 0
    Last Post: 11-05-2007, 06:39 PM
  3. Newbie in applet, Help me
    By barney in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:14 AM
  4. Help, java newbie
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 12:56 AM
  5. newbie: the app is loaded but i can't see it
    By tamayo in forum New To Java
    Replies: 1
    Last Post: 07-21-2007, 08:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •