Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,036 questions

52,001 answers

573 users

How to get the total drive size in Java

5 Answers

0 votes
package javaapplication1;

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.NumberFormat;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try {

            NumberFormat nf = NumberFormat.getNumberInstance();
            for (Path root : FileSystems.getDefault().getRootDirectories()) {

                System.out.print(root + ": ");
                FileStore store = Files.getFileStore(root);
                System.out.println(nf.format(store.getTotalSpace()) + " bytes");
            }

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
         
run:
   
C:\: 127,456,505,856 bytes
D:\: 1,000,202,039,296 bytes
E:\: 3,000,457,228,288 bytes
 
 */

 



answered Nov 18, 2016 by avibootz
0 votes
package javaapplication1;

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.NumberFormat;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try {

            NumberFormat nf = NumberFormat.getNumberInstance();
            for (Path root : FileSystems.getDefault().getRootDirectories()) {

                System.out.print(root + ": ");
                FileStore store = Files.getFileStore(root);
                System.out.println( nf.format(store.getTotalSpace()/1024) + " KB");
            }

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
         
run:
   
C:\: 124,469,244 KB
D:\: 976,759,804 KB
E:\: 2,930,134,012 KB
 
 */

 



answered Nov 18, 2016 by avibootz
0 votes
package javaapplication1;

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.NumberFormat;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try {

            NumberFormat nf = NumberFormat.getNumberInstance();
            for (Path root : FileSystems.getDefault().getRootDirectories()) {

                System.out.print(root + ": ");
                FileStore store = Files.getFileStore(root);
                System.out.println( nf.format(store.getTotalSpace()/(1024*1024)) + " MB");
            }

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
         
run:
   
C:\: 121,551 MB
D:\: 953,866 MB
E:\: 2,861,458 MB
 
 */

 



answered Nov 18, 2016 by avibootz
0 votes
package javaapplication1;

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.NumberFormat;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try {

            NumberFormat nf = NumberFormat.getNumberInstance();
            for (Path root : FileSystems.getDefault().getRootDirectories()) {

                System.out.print(root + ": ");
                FileStore store = Files.getFileStore(root);
                System.out.println( nf.format(store.getTotalSpace()/(1024*1024*1024)) + " GB");
            }

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
         
run:
   
C:\: 118 GB
D:\: 931 GB
E:\: 2,794 GB
 
 */

 



answered Nov 18, 2016 by avibootz
0 votes
package javaapplication1;

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try {

            for (Path root : FileSystems.getDefault().getRootDirectories()) {

                System.out.print(root + ": ");
                FileStore store = Files.getFileStore(root);
                long l = 1024L*1024L*1024L*1024L;
                System.out.println( (float)store.getTotalSpace()/l + " TB");
            }

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
         
run:
   
C:\: 0.11592102 TB
D:\: 0.90967846 TB
E:\: 2.7289 TB
 
 */

 



answered Nov 18, 2016 by avibootz
...