Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

Amount of free disk space ?

Thread view: 
entropy - 18 Mar 2005 18:47 GMT
I'm looking for a Java class that provides the ability to query the
underlying hardware for free space available.
Fahd Shariff - 19 Mar 2005 12:30 GMT
"I don't think there is a way to determine free disk space in a
platform-independant way. For each platform there exist a simple way to
determine free disk space - but there is no api function in java
available to do this.

A simple method or two for determining free disk space will be added to
the java.io.File class in Mustang, which is scheduled to ship in
mid-2006.

The only way to find free disk space appears to be by a 'guess'
algorithm that attempts to create different file sizes...not
acceptable."

Signature

Fahd Shariff

ninhoa@yahoo.es - 21 Mar 2005 12:19 GMT
Try this, call it by a loop with File.listRoots() or something
similar.
NOTE: this method is slooooooooooow.

   public long getFileSize(File fileOrDir, boolean includeSubDirs)
   {
       try
       {
           File f = fileOrDir;
           long theSize = 0L;
           
           int iDirs = 0;
           int iDocs = 0;
           File[] theFiles = f.listFiles(), dirs = new
File[theFiles.length], docs = new File[theFiles.length];
           for ( int i=0; i<theFiles.length; i++ )
           {
               if ( theFiles[i].isDirectory() )
                   dirs[iDirs++] = theFiles[i];
               else if ( theFiles[i].isFile() )
                   docs[iDocs++] = theFiles[i];
           }
           for ( int i=0; i<iDocs; i++ )
           {
               theSize += docs[i].length();
           }
           for ( int i=0; i<iDirs; i++ )
           {
               if (includeSubDirs)
                   getFileSize(dirs[i], includeSubDirs);
           }
       }
       catch (Exception e)
       {
           System.out.println(e.getMessage());
       }
       return theSize;
   }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.