Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Sigar获取Java中用于读取可用空间的磁盘列表_Java_Sigar - Fatal编程技术网

使用Sigar获取Java中用于读取可用空间的磁盘列表

使用Sigar获取Java中用于读取可用空间的磁盘列表,java,sigar,Java,Sigar,我需要获得系统中所有磁盘或所有分区的可用磁盘空间,我不介意。(我不必使用Sigar,但我已经在项目中为其他一些过程使用了它,所以我也可以使用它) 我使用的是sigarapi,得到了这个 public double getFreeHdd() throws SigarException{ FileSystemUsage f= sigar.getFileSystemUsage("/"); return ( f.getAvail()); } 但是这只给了我系统

我需要获得系统中所有磁盘或所有分区的可用磁盘空间,我不介意。(我不必使用Sigar,但我已经在项目中为其他一些过程使用了它,所以我也可以使用它) 我使用的是sigarapi,得到了这个

public double getFreeHdd() throws SigarException{
        FileSystemUsage f= sigar.getFileSystemUsage("/");
        return ( f.getAvail()); 
    }
但是这只给了我系统分区(root),我如何得到所有分区的列表并循环它们以获得它们的可用空间? 我试过这个

FileSystemView fsv = FileSystemView.getFileSystemView();
        File[] roots = fsv.getRoots();
        for (int i = 0; i < roots.length; i++) {
            System.out.println("Root: " + roots[i]);
        }
FileSystemView fsv=FileSystemView.getFileSystemView();
文件[]根=fsv.getRoots();
for(int i=0;i
但它只返回根目录

根目录:/

谢谢

编辑 看来我可以用
FileSystem[]fslist=sigar.getFileSystemList()

但是我得到的结果与我从终端得到的结果不匹配。另一方面,在我正在使用的这个系统上,我有3个磁盘,共有12个分区,所以我可能会丢失一些东西。我将在其他系统上尝试它,以防我能从结果中得到有用的东西。

您可以使用
java.nio.file.FileSystems
获取
java.nio.file.FileStorages
的列表,然后查看可用/可用空间。每个实例(假设您使用的是Java 7+):

import java.io.IOException;
导入java.nio.file.FileStore;
导入java.nio.file.FileSystem;
导入java.nio.file.FileSystems;
导入java.util.function.Consumer;
公共静态void main(字符串[]args){
FileSystem fs=FileSystems.getDefault();
fs.getFileStores().forEach(新使用者(){
@凌驾
公共作废接受(文件存储存储){
试一试{
System.out.println(store.getTotalSpace());
System.out.println(store.getUsableSpace());
}捕获(IOE异常){
e、 printStackTrace();
}
}
});
}

另外,请记住,
FileStore.getUsableSpace()
以字节为单位返回大小。有关更多信息,请参阅文档。

我们广泛使用SIGAR进行跨平台监控。这是我们用来获取文件系统列表的代码:

/**
* @return a list of directory path names of file systems that are local or network - not removable media
*/
public static Set<String> getLocalOrNetworkFileSystemDirectoryNames() {
  Set<String> ret = new HashSet<String>();
  try {
    FileSystem[] fileSystemList = getSigarProxy().getFileSystemList();

    for (FileSystem fs : fileSystemList) {
      if ((fs.getType() == FileSystem.TYPE_LOCAL_DISK) || (fs.getType() == FileSystem.TYPE_NETWORK)) {
        ret.add(fs.getDirName());
      }
    }
  }
  catch (SigarException e) {
    // log or rethrow as appropriate
  }

  return ret;
}
getSigarProxy()
只是一种方便的基本方法:

// The Humidor handles thread safety for a single instance of a Sigar object
static final private SigarProxy sigarProxy = Humidor.getInstance().getSigar();

static final protected SigarProxy getSigarProxy() {
  return sigarProxy;
}

Windows有
fsutil-fsinfo驱动器
提供
驱动器:C:\E:\F:\G:\
fsutil-fsinfo驱动器类型G:
提供
G:-CD-ROM驱动器
@Marichyasana我更喜欢使用与系统无关的驱动器。目前,这将在Linux中执行,但仍不知道将来是否会使用Win box。这就是我首先选择Sigar的原因谢谢你的提示,但出于某种原因,它没有读取我所有的分区。它少了一些,所以我用marcospereira的解决方案怪异。。。其他sigar方法是否适用于
getFileSystemList
未找到的分区路径?不,没有时间查看可能出现的问题,特别是因为我使用了其他方法。再次感谢
FileSystemUsage usageStats = getSigarProxy().getFileSystemUsage(fileSystemDirectoryPath);
// The Humidor handles thread safety for a single instance of a Sigar object
static final private SigarProxy sigarProxy = Humidor.getInstance().getSigar();

static final protected SigarProxy getSigarProxy() {
  return sigarProxy;
}