我知道我来晚了,但我写了一个只做它该做的事情的类。它所做的是读取/proc/mounts文件,将其解析为块设备(如/dev/sda)、挂载点、文件系统类型及其挂载选项。如果您不需要我编写的其他内容,可以将其排除在外,但对于我正在工作的项目,我想包括它,例如当前用户是否可以写入或从当前挂载点读取。
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class UnixPartition {
private final String block, mount, fsType, mntOptions;
private final long totalSpace, freeSpace, usableSpace;
private final File root;
protected UnixPartition(String block, String mount, String fsType, String mountOptions, long totalSpace, long freeSpace, long usableSpace, File root) {
this.block = block;
this.mount = mount;
this.fsType = fsType;
this.mntOptions = mountOptions;
this.totalSpace = totalSpace;
this.freeSpace = freeSpace;
this.usableSpace = usableSpace;
this.root = root;
}
public String getBlock() {
return block;
}
public String getMount() {
return mount;
}
public String getFilesystemType() {
return fsType;
}
public long getTotalSpace() {
return totalSpace;
}
public long getFreeSpace() {
return freeSpace;
}
public long getUsableSpace() {
return usableSpace;
}
public String getMountOptions() {
return mntOptions;
}
public boolean canCurrentUserWrite() {
return root.canWrite();
}
public boolean canCurrentUserRead() {
return root.canRead();
}
private String getLineSeparator(String contents) {
char[] chars = contents.toCharArray();
long r = 0;
long n = 0;
for (char c : chars) {
if (c == '\r')
r++;
if (c == '\n')
n++;
}
if (r == n)
return "\r\n";
else if (r >= 1 && n == 0)
return "\r";
else if (n >= 1 && r == 0)
return "\n";
else
return "";
}
public static List<UnixPartition> getPartitions(boolean filterSpecials) throws IOException {
String contents = new String(FileUtil.read("/proc/mounts"));
String[] lines = contents.split(getLineSeparator(contents));
List<UnixPartition> partitions = new ArrayList<>();
for (String line : lines) {
StringTokenizer tokenizer = new StringTokenizer(line, " ");
String blk = tokenizer.nextToken(),
mnt = tokenizer.nextToken(),
type = tokenizer.nextToken(),
opt = tokenizer.nextToken();
if (filterSpecials) {
if ((blk.contains("proc") || mnt.contains("proc") || type.contains("proc")) ||
(blk.contains("systemd") || mnt.contains("systemd") || type.contains("systemd")) ||
(blk.contains("binmft_misc") || mnt.contains("binmft_misc") || type.contains("binmft_misc")) ||
(blk.contains("udev") || mnt.contains("udev") || type.contains("udev")) ||
(blk.contains("devpts") || mnt.contains("devpts") || type.contains("devpts")) ||
(blk.contains("fuse") || mnt.contains("fuse") || type.contains("fuse")) ||
(blk.contains("pstore") || mnt.contains("pstore") || type.contains("pstore")) ||
type.contains("tmp"))
continue;
if (blk.contains("none"))
continue;
}
File root = new File(mnt);
partitions.add(new UnixPartition(blk, mnt, type, opt,
root.getTotalSpace(), root.getFreeSpace(), root.getUsableSpace(), root));
}
return partitions;
}
}
编辑:你可能需要自己过滤掉特殊的挂载类型,因为我只是从另一个答案中使用了它们。
df
和mount
可以了解已挂载的文件系统。请注意不要改变原文意思。 - Basile Starynkevitch