📌  相关文章
📜  使用Java查找可用磁盘空间

📅  最后修改于: 2022-05-13 01:54:39.192000             🧑  作者: Mango

使用Java查找可用磁盘空间

Java中有一些方法调用可用于获取有关磁盘驱动器的空间相关信息。这些用于获取此类信息的方法在 File 类中声明,该类位于Java.io 包中。下面给出了这些方法调用的详细信息及其使用:
注意:这些代码不会在在线 ide 上运行。此外,它们都适用于Java 1.6 及更高版本

  1. Java.io.File.getFreeSpace() :获取驱动器中可用的总可用空间
    句法:
    public long getFreeSpace()
    Returns the size of the partition named by this abstract pathname.
    Returns:
    The size, in bytes, of the partition or 0L if this 
    abstract pathname does not name a partition
    Throws:
    SecurityException - If a security manager has been installed and it 
    denies RuntimePermission("getFileSystemAttributes") or its 
    SecurityManager.checkRead(String) method denies read access to the file 
    named by this abstract pathname
    // Java program to get the amount of free space available on any drive
    import java.io.*;
      
    public class Test
    {
        public static void main(String[] args)
        {
            File file = new File("E:\\");
              
            double size = file.getFreeSpace() / (1024.0 * 1024 * 1024);
            System.out.printf( "%.3f GB\n", size);    
        }
    }
    

    输出:

    18.242 GB
  2. Java.io.File.getUsableSpace() :驱动器可用的总可用空间。返回此抽象路径名命名的分区上此虚拟机可用的字节数。如果可能,此方法会检查写入权限和其他操作系统限制,因此通常会比 getFreeSpace() 更准确地估计实际可以写入的新数据量。
    返回的可用字节数是一个提示,但不能保证可以使用这些字节中的大部分或任何一个。未分配字节数很可能在此调用后立即准确。任何外部 I/O 操作(包括在此虚拟机之外的系统上进行的操作)都可能使其不准确。此方法不保证对该文件系统的写入操作会成功。
    句法:
    public long getUsableSpace()
    Returns:
    The number of available bytes on the partition or 0L if the 
    abstract pathname does not name a partition. On systems where
    this information is not available, this method will be
    equivalent to a call to getFreeSpace().
    Throws:
    SecurityException - If a security manager has been installed and it 
    denies RuntimePermission("getFileSystemAttributes") or its
    SecurityManager.checkRead(String) method denies read access to the 
    file named by this abstract pathname
    import java.io.*;
      
    public class Test
    {
        public static void main(String[] args)
        {
            double size = 
                  new File("C:\\").getUsableSpace() / (1024.0 * 1024 * 1024);
            System.out.printf( "%.3f GB\n", size);    
        }
    }
    

    输出:

    62.857 GB
  3. Java.io.File.getTotalSpace() :驱动器的总容量。该方法返回由此抽象路径名命名的分区的大小。

    句法:

    public long getTotalSpace()
    Returns: Returns:
    The size, in bytes, of the partition or 0L if this abstract pathname 
    does not name a partition
    Throws:
    SecurityException - If a security manager has been installed and it
    denies RuntimePermission("getFileSystemAttributes") or its 
    SecurityManager.checkRead(String) method denies read access to 
    the file named by this abstract pathname
    import java.io.*;
      
    public class Test
    {
        public static void main(String[] args)
        {
            double size = 
                  new File("C:\\").getUsableSpace() / (1024.0 * 1024 * 1024);
            System.out.printf( "%.3f GB\n", size);    
        }
    }
    

    输出:

    62.857 GB

使用的参考: Oracle