Pergunta

Como descobrir quanto espaço de disco é deixado usando Java?

Foi útil?

Solução

Basta olhar para a classe de arquivo documentação. Este é um dos novos recursos em 1.6.

Esses novos métodos também incluem:

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

Se você ainda está usando 1.5, então você pode usar o Apache Commons io biblioteca e sua Classe de sistema de arquivos

Outras dicas

O Java 1.7 tem uma API ligeiramente diferente, o espaço livre pode ser consultado através do FileStore classe através do getTotalspace (), getUnallocatedspace () e getusableSpace () métodos.

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

    System.out.print(root + ": ");
    try {
        FileStore store = Files.getFileStore(root);
        System.out.println("available=" + nf.format(store.getUsableSpace())
                            + ", total=" + nf.format(store.getTotalSpace()));
    } catch (IOException e) {
        System.out.println("error querying space: " + e.toString());
    }
}

A vantagem desta API é que você recebe exceções significativas de volta ao consultar o espaço do disco falha.

Use CommonsIO and FilesystemUtils:

https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileSystemUtils.html#freeSpaceKb()

e.g.

FileSystemUtils.freeSpaceKb("/"); 

or built into the JDK:

http://java.sun.com/javase/6/docs/api/java/io/File.html#getFreeSpace()

new File("/").getFreeSpace();

Link

If you are a Java programmer, you may already have been asked this simple, stupide question: “how to find the free disk space left on my system?”. The problem is that the answer is system dependent. Actually, it is the implementation that is system dependent. And until very recently, there was no unique solution to answer this question, although the need has been logged in Sun’s Bug Database since June 1997. Now it is possible to get the free disk space in Java 6 with a method in the class File, which returns the number of unallocated bytes in the partition named by the abstract path name. But you might be interested in the usable disk space (the one that is writable). It is even possible to get the total disk space of a partition with the method getTotalSpace().

in checking the diskspace using java you have the following method in java.io File class

  • getTotalSpace()
  • getFreeSpace()

which will definitely help you in getting the required information. For example you can refer to http://javatutorialhq.com/java/example-source-code/io/file/check-disk-space-java/ which gives a concrete example in using these methods.

public class MemoryStatus{  
 public static void main(String args[])throws Exception{  
  Runtime r=Runtime.getRuntime();  
  System.out.println("Total Memory: "+r.totalMemory());  
  System.out.println("Free Memory: "+r.freeMemory());    
 }
}

Try this code to get diskspace

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top