How to Find How Much Disk Space Is Left Using Java

How to find how much disk space is left using Java?

Have a look at the File class documentation. This is one of the new features in 1.6.

These new methods also include:

  • public long getTotalSpace()
  • public long getFreeSpace()
  • public long getUsableSpace()

If you're still using 1.5 then you can use the Apache Commons IO library and its FileSystem class

How to check free disk space before file creation

In java 1.6

  • public long getTotalSpace()
  • public long getFreeSpace()

Check this document

In java 1.7

  • getTotalSpace()
  • getUnallocatedSpace()
  • getUsableSpace()

Check this document

In java 8

  • getFreeSpace()
  • getUsableSpace()

Check this document

Please check this Example if you need some help for code

Edit: I got this intresting bug while searching for your problem.
Please have a look at this.PrintWriter does not throw IOException for full floppy disk

Calculate usable and total disk space using java

To calculate the disk size on your remote machine, you need a connection to your remote machine (obviously). The difficult question is how to establish this connection. As for any problem, there are some different solutions:

Mount the drive as a remote folder

If you can (I don't know if you can or want) mount the remote folder as a shared folder on your Windows machine. If you do so, you can just use our code as it is by changing the path of the file to the mounted folder. But this solution could be unpracticle and is not a "Java" solution.

Use an agent on your remote machine

Windows lacks of a solution like Linux's SSH to connect to a machine. So there is no easy way to connect to Windows machine. There are some tools like IBM's STAF (see here) which address this problem. You install STAF on the remote machine and on your local machine and can connect trough it. But it is a pretty ugly solution and is super insecure. But for a test system maybe enough.

Write your own service

There is no way to connect to a Windows machine (correct me if I am wrong) and tools like STAF are just bad workarounds. As you also concern about performance, I suggest you just deploy your tool to your Windows machine and provide a JSON-API on top of it. So you can take your browser, go to the machine and see how much space is left.

I know that's not what you hopped to hear, but you will have a bad time when you try to connect to a Windows machine...

How to find disk space of remote linux machine using Java

The easiest way to get the remote machine information and also safe way to do
is to SSH to the server from Java and get the necessary information from.

use JSch Library for SSH connection to server.

for an example look here

How do you find how much disk space is left in Cocoa?

Use -[NSFileManager attributesOfFileSystemForPath:error:]



Related Topics



Leave a reply



Submit