Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-31-2008, 01:41 AM
Member
 
Join Date: Mar 2008
Location: wales
Posts: 2
vicky08 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-31-2008, 06:36 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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()); } } } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-31-2008, 06:26 PM
Member
 
Join Date: Mar 2008
Location: wales
Posts: 2
vicky08 is on a distinguished road
Thank-you very much for all your help.

Vicky
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie CSnoob87 Introductions 2 02-18-2008 10:49 AM
Newbie reporting nelsaez Introductions 0 11-05-2007 08:39 PM
Newbie in applet, Help me barney Java Applets 1 08-07-2007 09:14 AM
Help, java newbie baltimore New To Java 1 08-07-2007 02:56 AM
newbie: the app is loaded but i can't see it tamayo New To Java 1 07-21-2007 10:14 AM


All times are GMT +3. The time now is 12:52 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org