使用Java读取所有linux分区
我发现了这个Java代码,它用于获取Linux中的所有分区使用Java读取所有linux分区,java,linux,jakarta-ee,Java,Linux,Jakarta Ee,我发现了这个Java代码,它用于获取Linux中的所有分区 public class Partitions { // output // [8, 0, 312571224, sda, 8, 1, 716800, sda1, 8, 2, 5120000, sda2, 8, 3, 306733056, sda3] public static final String partitionsPath = "/proc/partitions"; public static
public class Partitions
{
// output
// [8, 0, 312571224, sda, 8, 1, 716800, sda1, 8, 2, 5120000, sda2, 8, 3, 306733056, sda3]
public static final String partitionsPath = "/proc/partitions";
public static final String EMPTY = "";
public static final String SPACE = " ";
public static final String LINE_SEPARATOR = "line.separator";
public ArrayList<String> gatherPartitionUsage()
{
ArrayList<String> data = new ArrayList<>();
String[] tempData = null;
String[] tempFile = null;
tempFile = getContents(partitionsPath).split(System.getProperty(LINE_SEPARATOR));
//parse the disk partitions
for (int i = 2; i < tempFile.length; i++)
{
tempData = tempFile[i].split(SPACE);
data.addAll(Arrays.asList(tempData));
data.removeAll(Collections.singleton(EMPTY));
}
return data;
}
static private synchronized String getContents(String path)
{
//...checks on aFile are elided
StringBuilder contents = new StringBuilder();
try
{
//use buffering, reading one line at a time
//FileReader always assumes default encoding is OK!
BufferedReader input = new BufferedReader(new FileReader(new File(path)));
try
{
String line = null; //not declared within while loop
/*
* readLine is a bit quirky : it returns the content of a line
* MINUS the newline. it returns null only for the END of the
* stream. it returns an empty String if two newlines appear in
* a row.
*/
while ((line = input.readLine()) != null)
{
contents.append(line);
contents.append(System.getProperty(LINE_SEPARATOR));
}
}
finally
{
input.close();
}
}
catch (IOException ex)
{
}
return contents.toString();
}
/*
* Create a list with the partitions name to be used to find their
* statistics in /proc/diskstats file
*/
private ArrayList<String> getPartitionNames(ArrayList<String> data)
{
ArrayList<String> partitionsName = new ArrayList<>();
for (String string : data)
{
if (!tryParseInt(string))
{
partitionsName.add(string);
}
}
return partitionsName;
}
/**
* Try to parse a string to a integer.
*
* @param value
* @return True if the string can be converted, false otherwise.
*/
public boolean tryParseInt(String value)
{
try
{
Integer.parseInt(value);
return true;
}
catch (NumberFormatException ex)
{
// Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
}
公共类分区
{
//输出
//[8,0,312571224,sda,8,1,716800,sda1,8,2,5120000,sda2,8,3,306733056,sda3]
公共静态最终字符串partitionsPath=“/proc/partitions”;
公共静态最终字符串为空=”;
公共静态最终字符串空间=”;
公共静态最终字符串行\u SEPARATOR=“LINE.SEPARATOR”;
公共ArrayList gatherPartitionUsage()
{
ArrayList数据=新的ArrayList();
字符串[]tempData=null;
字符串[]tempFile=null;
tempFile=getContents(partitionsPath).split(System.getProperty(行分隔符));
//解析磁盘分区
for(int i=2;i
这应该是输出
[8,0,312571224,sda,8,1,716800,sda1,8,2,5120000,sda2,8,3,306733056,sda3]
你能告诉我这是对的吗?看起来它现在起作用了。还有,我如何将返回的结果保存在Java映射中?当您运行程序时,输出是什么,或者您是否要求我们为您运行此程序?嗯,这是高度系统机密性的。您需要知道,它可能无法在所有系统上工作(proc未安装、chroot或容器隔离)。此外,如果您对潜在的或现有的挂载点感兴趣,那么分区可能就没那么有用了(想想LVM和其他人)。如果您对现有的挂载文件系统更感兴趣,可以选择/proc/mounts,甚至
filesystems.getDefault().getFilestores()
I获得与本文类似的结果。你能运行代码并查看结果吗?@eckes你能告诉我一些代码示例吗?@user1285928告诉我们你对什么感兴趣,出于什么目的可能是个好主意。如果您想编写一个用于分区操作的前端,那么分叉“lsblk”并读取其输出会更好。