Dojo_user
Dojo_user

Reputation: 281

How can I calclulate the diskspace of a remote machine using java?

Can anyone suggest me how can I modify the below code which is currently calculating the free disk space(C:) of my local system(Windows OS) to calculate the free disk space (C:) of the remote machine(Windows OS) using java?

import java.io.File;

public class DiskMemory
{
    public static void main(String[] args)
    {   
        File file = new File("C:");
        //total disk space in bytes.
        long totalSpace = file.getTotalSpace(); 
        //unallocated free disk space in bytes.
        long usableSpace = file.getUsableSpace(); 
        //unallocated free disk space available to current user.
        long freeSpace = file.getFreeSpace(); 



        System.out.println(" ==Total Memory Allocation == ");
        System.out.println("Total size : " + totalSpace + " bytes");
        System.out.println("Space free : " + usableSpace + " bytes");
        System.out.println("Space free : " + freeSpace + " bytes");

        System.out.println(" === mega bytes ===");
        System.out.println("Total size : " + totalSpace /1024 /1024 + " mb");
        System.out.println("Space free : " + usableSpace /1024 /1024 + " mb");
        System.out.println("Space free : " + freeSpace /1024 /1024 + " mb");
    }
}

Upvotes: 2

Views: 3719

Answers (3)

aetherwalker
aetherwalker

Reputation: 131

After looking at Apache's VFS, I found it overly complicated to get disk space usage, and it relies on jCIFS ( https://jcifs.samba.org/ ), which will do it with less hassel.

SmbFile file = new SmbFile("smb://[user]:[pass]@[server]/[share]");
System.out.println(file.getDiskFreeSpace()); // Space free on share
System.out.println(file.length()); // Total space available on share

Upvotes: 3

user3611877
user3611877

Reputation: 64

You could look into using commons VFS for accessing the remote file systems and determining the file size

http://commons.apache.org/proper/commons-vfs/

This would allow you to get a file representing the remote directory (FileObject in VFS lingo) and get the size much in the same way you are doing already. Have a look at the website for an example. You could use VFS to query your local disk space as well.

Upvotes: 1

Aman
Aman

Reputation: 9015

Since you have access to all the machines whose disk space you want to calculate, you can write a simple client server program to solve the problem. The high level overview of the system will be the following :

Let C be your machine, let s1, s2, s3, ..., sN be the N machines whose disk space you want on C.

On each the s machines, (s1, s2, etc), you need to run a server which is listening on some port (choose a port > 1024). On machine C, there should be a client which will connect to all the s machines to get the usage.

Look up client server programming in java, it's pretty straightforward.

Upvotes: 0

Related Questions