📄 sigarfsinfo.java
字号:
/* * SigarFsInfo.java * * Created on March 14, 2005, 12:27 PM */package net.hyperic.sigar.cmd;import net.hyperic.sigar.Sigar;import net.hyperic.sigar.SigarException;import net.hyperic.sigar.FileSystem;import net.hyperic.sigar.FileSystemMap;import net.hyperic.sigar.FileSystemUsage;/** * * @author Sean */public class SigarFsInfo extends SigarCommandBase { private int _currFsIndex; private int _currFsUsageIndex; /** Creates a new instance of SigarFsInfo */ public SigarFsInfo() { super(); resetIterators(); } /** * */ public void resetIterators() { _currFsIndex = 0; _currFsUsageIndex = 0; } /** * */ public int getFsCount() throws SigarException { FileSystem[] fsListArr; try { fsListArr = this.proxy.getFileSystemList(); } catch (SigarException e) { throw e; } return fsListArr.length; } /** * */ public FileSystem getFileSystem(int iIndex) throws SigarException { FileSystem[] fsListArr; try { fsListArr = this.proxy.getFileSystemList(); } catch (SigarException e) { throw e; } if (iIndex < 0 || iIndex > fsListArr.length || fsListArr.length == 0) { return null; } return fsListArr[iIndex]; } /** * */ public FileSystem getNextFileSystem() throws SigarException { FileSystem fs; try { fs = getFileSystem(_currFsIndex); } catch (SigarException e) { _currFsIndex = 0; throw e; } if (fs == null) { _currFsIndex = 0; return null; } _currFsIndex++; return fs; } /** * */ public FileSystemUsage getFileSystemUsage(int iIndex) throws SigarException { FileSystem[] fsListArr; FileSystemUsage fsUsage; try { fsListArr = this.proxy.getFileSystemList(); } catch (SigarException e) { throw e; } if (iIndex < 0 || iIndex > fsListArr.length || fsListArr.length == 0) { return null; } try { fsUsage = this.sigar.getFileSystemUsage(fsListArr[iIndex].getDirName()); } catch (SigarException e) { throw e; } return fsUsage; } /** * */ public FileSystemUsage getNextFileSystemUsage() throws SigarException { FileSystemUsage fsUsage; try { fsUsage = getFileSystemUsage(_currFsUsageIndex); } catch (SigarException e) { _currFsUsageIndex = 0; throw e; } if (fsUsage == null) { _currFsUsageIndex = 0; return null; } _currFsUsageIndex++; return fsUsage; } /** * print filesystem info to stdout && test accessor methods */ public void output() throws SigarException { try { output(null); } catch (SigarException e) { throw e; } } /** * print filesystem info to stdout && test accessor methods */ public void output(String[] args) throws SigarException { FileSystem fs; FileSystemUsage fsUsage; String[] header = new String[] { "Filesystem", "Size", "Used", "Avail", "Use%", "Mounted on", "Type" }; long used, avail, total, pct; setOutputFormat("%-10s %4s %4s %5s %4s %-10s %s"); printf(header); try { for (int i=0; i<getFsCount(); i++) { fs = getFileSystem(i); try { fsUsage = getFileSystemUsage(i); used = fsUsage.getTotal() - fsUsage.getFree(); avail = fsUsage.getAvail(); total = fsUsage.getTotal(); pct = (long)(fsUsage.getUsePercent() * 100); } catch (SigarException e) { //e.g. on win32 D:\ fails with "Device not ready" //if there is no cd in the drive. used = avail = total = pct = 0; } String[] items = new String[7]; items[0] = fs.getDevName(); items[1] = Sigar.formatSize(total); items[2] = Sigar.formatSize(used); items[3] = Sigar.formatSize(avail); items[4] = String.valueOf(pct) + "%"; items[5] = fs.getDirName(); items[6] = fs.getSysTypeName() + "/" + fs.getTypeName(); printf(items); } } catch (SigarException e) { throw e; } } public static void main( String[] args ) { try { SigarFsInfo fsInfo = new SigarFsInfo(); fsInfo.output(); } catch ( Exception ex ) { System.out.println( ex.getMessage() ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -