Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 347187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:14:50+00:00 2026-05-12T11:14:50+00:00

I need to find the total size of a drive in Java 5 (or

  • 0

I need to find the total size of a drive in Java 5 (or 1.5, whatever). I know that Java 6 has a new method in java.io.File, but I need it to work in Java 5.

Apache Commons IO has org.apache.commons.io.FileSystemUtils to provide the free disk space, but not the total disk space.

I realize this is OS dependant and will need to depend on messy command line invocation. I’m fine with it working on “most” systems, i.e. windows/linux/macosx. Preferably I’d like to use an existing library rather than write my own variants.

Any thoughts? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:14:50+00:00Added an answer on May 12, 2026 at 11:14 am

    Update:

    I apologise for misreading the question, I recommend copying the FileSystemUtils approach, but modifying the commands it runs slightly.

    In dos you can get the free and total bytes with the fsutil command:

    fsutil volume diskfree [drive letter]
    

    on my box this gives the following results:

    Total # of free bytes        : 41707524096
    Total # of bytes             : 80023715840
    Total # of avail free bytes  : 41707524096
    

    On Unix, the command is still "df -k", you’re just interested in the "1024-blocks" column to the left of "Free" (example from Wikipedia below). You obviously need to multiply the result by 1024.

    Filesystem    1024-blocks      Free %Used    Iused %Iused Mounted on
    /dev/hd4            32768     16016   52%     2271    14% /
    /dev/hd2          4587520   1889420   59%    37791     4% /usr
    /dev/hd9var         65536     12032   82%      518     4% /var
    /dev/hd3           819200    637832   23%     1829     1% /tmp
    /dev/hd1           524288    395848   25%      421     1% /home
    /proc                   -         -    -         -     -  /proc
    /dev/hd10opt        65536     26004   61%      654     4% /opt
    

    Assuming you copy FileSystemUtils to implement "totalSpaceKB()" to delegate to an equivalent OS-specific method. The implementation for Windows would be something like this (note the use of "Find" to trim the output from fsutil to just get the total size):

    long totalSpaceWindows(String path) throws IOException {
        path = FilenameUtils.normalize(path);
        if (path.length() > 2 && path.charAt(1) == ':') {
            path = path.substring(0, 2); // seems to make it work
        }
    
        // build and run the 'fsutil' command
        String[] cmdAttribs = new String[] {
                "cmd.exe",
                "/C",
                "fsutil volume diskfree " + path
                        + " | Find \"Total # of bytes\"" };
    
        // read in the output of the command to an ArrayList
        List lines = performCommand(cmdAttribs, Integer.MAX_VALUE);
    
        //because we used Find, we expect the first line to be "Total # of bytes",
        //you might want to add some checking to be sure
        if (lines.size() > 0) {
            String line = (String) lines.get(0);
            String bytes = line.split(":")[1].trim();
            return Long.parseLong(bytes);
        }
        // all lines are blank
        throw new IOException(
                "Command line 'fsutil volume diskfree' did not return 
                        + "any info  for path '" + path + "'");
    }
    

    The implementation for Unix would be the same as freeSpaceUnix(), but remove the two calls to tok.nextToken() at the end of the method

        /** comment these two lines so the token received is the total size */
        tok.nextToken(); // Ignore 1K-blocks
        tok.nextToken(); // Ignore Used
        /** this will now be the total size */
        String freeSpace = tok.nextToken();
        return parseBytes(freeSpace, path);
    }
    

    The implementations for other platforms would be similar.

    Hope this helps and apologies agian for misreading the problem.


    Original answer (gets free bytes, not total).

    Prior to Java 6 there isn’t an elegant way to do this, (see the bug). Rather than rolling your own, I’d recommend using a library to do the platform-specific processing for you.

    Apache commons-io has a FileSystemUtils type that provides a static method freeSpaceKb(). It works on Windows and and some Unix implementations (see quote from Javadoc below)

    From the Javadoc:

    public static long freeSpaceKb(String path)
    throws IOException

    Returns the free space on a drive or volume in kilobytes by invoking the command line.
    FileSystemUtils.freeSpaceKb("C:"); // Windows
    FileSystemUtils.freeSpaceKb("/volume"); // *nix

    The free space is calculated via the command line. It uses ‘dir /-c’ on Windows, ‘df -kP’ on AIX/HP-UX and ‘df -k’ on other Unix.
    In order to work, you must be running Windows, or have a implementation of Unix df that supports GNU format when passed -k (or -kP). If you are going to rely on this code, please check that it works on your OS by running some simple tests to compare the command line with the output from this class. If your operating system isn’t supported, please raise a JIRA call detailing the exact result from df -k and as much other detail as possible, thanks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find the underlying disk capacity (total size) of an unmapped network
I have an XML file of size 31 GB. I need to find the
I need to find the 'name of the branch that has made the most
I need to find/create an application that will create employee web usage reports from
I need to find all occurrences of a function call in a C++ file
I need to find the size of a JPEG (JFIF) image. The image is
I need to find the total timings for the execution of a program over
I need to find the time difference in seconds with python. I know I
I need to develop a file indexing application in python and wanted to know
I'm pretty new to Scala but I like to know what is the preferred

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.