Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

595
Visualizações
How can a Java program list all partitions and get the free space of them on Linux?

I want the name of the partition with their total space, used space and free space for the Linux system using java program.

I am getting correct value in case of Windows system but in Linux I am getting only one drive information:

Here is what I have tried so far.

public class DiskSpace {

public static void main(String[] args) {
    FileSystemView fsv = FileSystemView.getFileSystemView();
    File[] drives = File.listRoots();
    if (drives != null && drives.length > 0) {
        for (File aDrive : drives) {
            System.out.println("Drive Letter: " + aDrive);
            System.out.println("\tType: " + fsv.getSystemTypeDescription(aDrive));
            System.out.println("\tTotal space: " + aDrive.getTotalSpace());
            System.out.println("\tFree space: " + aDrive.getFreeSpace());
            System.out.println();
        }
    }
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

There are no drive letters on Linux. If you want to know which partitions there are, and where they are mounted, read /proc/mounts. When you have a mount point (2nd column in /proc/mounts), use new File(mountpoint).getTotalSpace() to get the total space.

over 4 years ago · Santiago Trujillo Relatório

0

Linux, Unix, and Unix-like systems have one filesystem with one root within which there may be multiple mount points at which partitions containing Unix filesystems, partial or complete, may be mounted -- non-Unix filesystems may also be mounted with the appropriate software available to handle the transformations necessary, but the unified, single-root filesystem model remains.

If the FileSystemView class you are using is from the javax.swing.filechooser package, don't expect too much:

FileSystemView is JFileChooser's gateway to the file system. Since the JDK1.1 File API doesn't allow access to such information as root partitions, file type information, or hidden file bits, this class is designed to intuit as much OS-specific file system information as possible.

Java Licensees may want to provide a different implementation of FileSystemView to better handle a given operating system.

That second paragraph is key.

Java's virtual machine implementation is meant to abstract away the very kind of platform specific things you want in this case. To be successful, you will need to write or find native call wrapper classes for the native system API of each platform you will support. "High-level" abstractions like the FileSystemView class are unlikely to be complete or reliable in providing the information you need.

over 4 years ago · Santiago Trujillo Relatório

0

This is a code snippet to display name mounted external media only.

String OS = System.getProperty("os.name");
if(OS.equals("Linux"))
{
    String s = "";
    Runtime rt = Runtime.getRuntime();
    int n=0;
    Process ps = rt.exec("ls /run/media/Rancho");// Write your UserName, mine is Rancho

    InputStream in = ps.getInputStream();
    while((n = in.read())!=-1)
    {
        char ch = (char) n;
        s+ = ch;
    }
    System.out.println(s);  
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda