如何在Unix上获取所有已装入的Java文件系统的列表?
我在Unix平台上运行Java。如何通过Java1.6API获取所有已装入文件系统的列表 我尝试了如何在Unix上获取所有已装入的Java文件系统的列表?,java,unix,filesystems,Java,Unix,Filesystems,我在Unix平台上运行Java。如何通过Java1.6API获取所有已装入文件系统的列表 我尝试了File.listRoots(),但它返回一个文件系统(即/)。如果我使用df-h我会看到更多: Filesystem Size Used Avail Capacity iused ifree %iused Mounted on /dev/disk0s2 931Gi 843Gi 87Gi 91% 221142498 22838244 91% /
File.listRoots()
,但它返回一个文件系统(即/
)。如果我使用df-h
我会看到更多:
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
/dev/disk0s2 931Gi 843Gi 87Gi 91% 221142498 22838244 91% /
devfs 187Ki 187Ki 0Bi 100% 646 0 100% /dev
map -hosts 0Bi 0Bi 0Bi 100% 0 0 100% /net
map auto_home 0Bi 0Bi 0Bi 100% 0 0 100% /home
/dev/disk1s2 1.8Ti 926Gi 937Gi 50% 242689949 245596503 50% /Volumes/MyBook
/dev/disk2 1.0Gi 125Mi 875Mi 13% 32014 223984 13% /Volumes/Google Earth
我希望也能看到
/home
(至少)。Java不提供对装载点的任何访问。您必须通过Runtime.exec()
运行系统命令mount
,并解析其输出。或者解析/etc/mtab
的内容,@Cozzamara指出,我已经在使用mount
了。我最后得到的是:
// get the list of mounted filesystems
// Note: this is Unix specific, as it requires the "mount" command
Process mountProcess = Runtime.getRuntime ().exec ( "mount" );
BufferedReader mountOutput = new BufferedReader ( new InputStreamReader ( mountProcess.getInputStream () ) );
List<File> roots = new ArrayList<File> ();
while ( true ) {
// fetch the next line of output from the "mount" command
String line = mountOutput.readLine ();
if ( line == null )
break;
// the line will be formatted as "... on <filesystem> (...)"; get the substring we need
int indexStart = line.indexOf ( " on /" );
int indexEnd = line.indexOf ( " ", indexStart );
roots.add ( new File ( line.substring ( indexStart + 4, indexEnd - 1 ) ) );
}
mountOutput.close ();
//获取已装载文件系统的列表
//注意:这是特定于Unix的,因为它需要“mount”命令
Process mountproces=Runtime.getRuntime().exec(“mount”);
BufferedReader mountOutput=新的BufferedReader(新的InputStreamReader(mountProcess.getInputStream());
列表根=新的ArrayList();
while(true){
//从“mount”命令获取下一行输出
stringline=mountOutput.readLine();
如果(行==null)
打破
//行的格式为“…on(…)”,获取我们需要的子字符串
int indexStart=line.indexOf(“on/”);
int indexEnd=line.indexOf(“,indexStart);
root.add(新文件(line.substring(indexStart+4,indexEnd-1));
}
mountOutput.close();
您可以尝试使用以下方法解决问题:
我的代码
public List<String> getHDDPartitions() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/mounts"), "UTF-8"));
String response;
StringBuilder stringBuilder = new StringBuilder();
while ((response = bufferedReader.readLine()) != null) {
stringBuilder.append(response.replaceAll(" +", "\t") + "\n");
}
bufferedReader.close();
return Lists.newArrayList(Arrays.asList(stringBuilder.toString().split("\n")));
} catch (IOException e) {
LOGGER.error("{}", ExceptionWriter.INSTANCE.getStackTrace(e));
}
return null;
}
public List<Map<String, String>> getMapMounts() {
List<Map<String, String>> resultList = Lists.newArrayList();
for (String mountPoint : getHDDPartitions()) {
Map<String, String> result = Maps.newHashMap();
String[] mount = mountPoint.split("\t");
result.put("FileSystem", mount[2]);
result.put("MountPoint", mount[1]);
result.put("Permissions", mount[3]);
result.put("User", mount[4]);
result.put("Group", mount[5]);
result.put("Total", String.valueOf(new File(mount[1]).getTotalSpace()));
result.put("Free", String.valueOf(new File(mount[1]).getFreeSpace()));
result.put("Used", String.valueOf(new File(mount[1]).getTotalSpace() - new File(mount[1]).getFreeSpace()));
result.put("Free Percent", String.valueOf(getFreeSpacePercent(new File(mount[1]).getTotalSpace(), new File(mount[1]).getFreeSpace())));
resultList.add(result);
}
return resultList;
}
private Integer getFreeSpacePercent(long total, long free) {
Double result = (Double.longBitsToDouble(free) / Double.longBitsToDouble(total)) * 100;
return result.intValue();
}
public List getHDDPartitions(){
试一试{
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(新文件InputStream(“/proc/mounts”),“UTF-8”);
字符串响应;
StringBuilder StringBuilder=新的StringBuilder();
而((response=bufferedReader.readLine())!=null){
追加(response.replaceAll(“+”,“\t”)+“\n”);
}
bufferedReader.close();
return Lists.newArrayList(Arrays.asList(stringBuilder.toString().split(“\n”));
}捕获(IOE异常){
LOGGER.error(“{}”,ExceptionWriter.INSTANCE.getStackTrace(e));
}
返回null;
}
公共列表getMapMounts(){
List resultList=Lists.newArrayList();
对于(字符串装入点:getHDDPartitions()){
Map result=Maps.newHashMap();
字符串[]mount=mountPoint.split(“\t”);
put(“文件系统”,mount[2]);
结果。放置(“装入点”,装入[1]);
结果.put(“权限”,mount[3]);
结果.put(“用户”,mount[4]);
结果。放置(“组”,安装[5]);
result.put(“Total”,String.valueOf(新文件(mount[1]).getTotalSpace());
result.put(“Free”,String.valueOf(新文件(mount[1]).getFreeSpace());
result.put(“Used”,String.valueOf(新文件(挂载[1]).getTotalSpace()-新文件(挂载[1]).getFreeSpace());
result.put(“Free Percent”,String.valueOf(getFreeSpacePercent(新文件(mount[1]).getTotalSpace(),新文件(mount[1]).getFreeSpace());
结果列表。添加(结果);
}
返回结果列表;
}
私有整数getFreeSpacePercent(长时间总计,长时间空闲){
Double result=(Double.longBitsToDouble(免费)/Double.longBitsToDouble(总))*100;
返回result.intValue();
}
您可以使用JNA调用getmntent函数(使用“man getmntent”获取更多信息)
下面是一些示例代码,可以帮助您开始:
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
public class MntPointTest {
public static class mntent extends Structure {
public String mnt_fsname; //Device or server for filesystem
public String mnt_dir; //Directory mounted on
public String mnt_type; //Type of filesystem: ufs, nfs, etc.
public String mnt_opts;
public int mnt_freq;
public int mnt_passno;
@Override
protected List getFieldOrder() {
return Arrays.asList("mnt_fsname", "mnt_dir", "mnt_type", "mnt_opts", "mnt_freq", "mnt_passno");
}
}
public interface CLib extends Library {
CLib INSTANCE = (CLib) Native.loadLibrary("c", CLib.class);
Pointer setmntent(String file, String mode);
mntent getmntent(Pointer stream);
int endmntent(Pointer stream);
}
public static void main(String[] args) {
mntent mntEnt;
Pointer stream = CLib.INSTANCE.setmntent("/etc/mtab", "r");
while ((mntEnt = CLib.INSTANCE.getmntent(stream)) != null) {
System.out.println("Mounted from: " + mntEnt.mnt_fsname);
System.out.println("Mounted on: " + mntEnt.mnt_dir);
System.out.println("File system type: " + mntEnt.mnt_type);
System.out.println("-------------------------------");
}
CLib.INSTANCE.endmntent(stream);
}
}
在Java7+中,您可以使用nio
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
public class ListMountedVolumesWithNio {
public static void main(String[] args) throws IOException {
for (FileStore store : FileSystems.getDefault().getFileStores()) {
long total = store.getTotalSpace() / 1024;
long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024;
long avail = store.getUsableSpace() / 1024;
System.out.format("%-20s %12d %12d %12d%n", store, total, used, avail);
}
}
}
OSHI(Java操作系统和硬件信息库)在这里很有用:
查看此代码:
@Test
public void test() {
final SystemInfo systemInfo = new SystemInfo();
final OSFileStore[] fileStores = systemInfo.getOperatingSystem().getFileSystem().getFileStores();
Stream.of(fileStores)
.peek(fs ->{
System.out.println("name: "+fs.getName());
System.out.println("type: "+fs.getType() );
System.out.println("str: "+fs.toString() );
System.out.println("mount: "+fs.getMount());
System.out.println("...");
}).count();
}
家庭在中间,如果家里安装了另一个设备,只会显示出来。但是
File.listRoots()
不会返回/home
。这就是重点,它应该是吗/
是unix文件系统的唯一真正根。如果你说你在用这个函数做什么可能会更好?我理解,但是挂载的文件系统列表与文件系统名称空间不同。在我的Mac OS X机器(Mountain Lion)上没有/etc/mtab
,但在我的Ubuntu服务器上有。OS X在许多方面与“真正的”Unix有很大的不同,因此,任何特定于操作系统的事情,比如这件事,都可能需要以不同的方式来完成。在任何情况下,mount
似乎都是一个通用工具,应该也适用于Mac。对于类似的任务,我使用了df
,这同样有效。在OS X上,您可以使用(新文件(“/Volumes”))解析/Volumes的内容。listFiles()
。其中一个条目有一个指向/
的符号链接,您可以使用getCanonicalPath()
进行测试。您还必须测试并排除不是装入点的文件,比如讨厌的.DS_Store
。对于java7+,请查看以下行:root.add(新文件(line.substring(indexStart+4,indexEnd-1))
be:root.add(新文件(line.substring(indexStart+4,indexEnd))代码>?谢谢你的回答!你可能知道有没有一个项目或库可以将所有常用方法映射到java(我的意思是像mount/umount和friends)?我正在做一个项目,它可以对关键的操作系统本机代码(内存、文件、cpu、电池等)进行这种基于JNA的映射。这是一个很好的选择!不幸的是,从文件存储区到路径/文件没有简单的方法。解析toString()是一个可能会中断的黑客解决方法……这项工作做得很好。我不知道你为什么没有更多的选票。干得好