Portable way to determine total disk size in Java (pre java 6)

I need to find the total disk size in Java 5 (or 1.5, regardless). 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 free disk space, but not full disk space.

I understand that this is OS dependent and will need to depend on a promiscuous command line call. I am fine working on the "majority" of systems, that is windows / linux / macosx. I prefer using the existing library rather than writing my own options.

Any thoughts? Thanks.

+3
source share
3 answers

Update:

, FileSystemUtils, .

dos fsutil:

fsutil volume diskfree [drive letter]

:

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

Unix - "df -k", "1024 " "Free" ( ). , 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

, FileSystemUtils "totalSpaceKB()", , . Windows - ( "", fsutil, ):

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 + "'");
}

Unix , freeSpaceUnix(), tok.nextToken()

    /** 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);
}

.

, , .


( , ).

Java 6 (. bug). , , .

Apache commons-io FileSystemUtils, freeSpaceKb(). Windows Unix (. Javadoc )

Javadoc:

public static long freeSpaceKb (String path)                          IOException

, .  FileSystemUtils.freeSpaceKb( "C:" );//Windows  FileSystemUtils.freeSpaceKb( "/" );//* nix

. "dir/-c" Windows, "df -kP" AIX/HP-UX "df -k" Unix. , Windows Unix df, GNU -k ( -kP). , , , , . , , JIRA-, df -k , .

+4

.

, API/. , , , , Ant script Ant , , . Ant? - :

, , API Ant, . - , Ant .

, - , .

- - API.

+1

SUN . JConfig . , (), , , , , :

It allows you to work with files, web sites of browsers, processes, file types, and other system-level elements in a much more advanced way than that provided by the standard Java library class. For example, you can use it to launch web browsers or other external applications instead of using Runtime.exec or solutions that only work on one platform.

So, if you are patient and happy ...

0
source

Source: https://habr.com/ru/post/1712662/