JAVA实现将以下字符串Filesystem Size Used Avail Use% Mounted on devtmpfs 926M 0 926M 0% /dev tmpfs 944M 0 944M 0% /dev/shm tmpfs 944M 440K 944M 1% /run tmpfs 944M 0 944M 0% /sys/fs/cgroup /dev/vda1 40G 2.9G 35G 8% / tmpfs 189M 0 189M 0% /run/user/0 根据换行及回车转换为转换为List<SysFile>对象,跳过第一行标题
时间: 2023-09-16 20:16:00 浏览: 116
好的,以下是 JAVA 实现将该字符串转换为 List<SysFile> 对象的代码:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String input = "Filesystem\tSize\tUsed\tAvail\tUse%\tMounted on\n" +
"devtmpfs\t926M\t0\t926M\t0%\t/dev\n" +
"tmpfs\t944M\t0\t944M\t0%\t/dev/shm\n" +
"tmpfs\t944M\t440K\t944M\t1%\t/run\n" +
"tmpfs\t944M\t0\t944M\t0%\t/sys/fs/cgroup\n" +
"/dev/vda1\t40G\t2.9G\t35G\t8%\t/\n" +
"tmpfs\t189M\t0\t189M\t0%\t/run/user/0\n";
List<SysFile> sysFiles = new ArrayList<>();
try (Scanner scanner = new Scanner(input)) {
// 跳过第一行
if (scanner.hasNextLine()) {
scanner.nextLine();
}
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] fields = line.split("\\s+");
SysFile sysFile = new SysFile();
sysFile.setFilesystem(fields[0]);
sysFile.setSize(fields[1]);
sysFile.setUsed(fields[2]);
sysFile.setAvail(fields[3]);
sysFile.setUsePercentage(fields[4]);
sysFile.setMountedOn(fields[5]);
sysFiles.add(sysFile);
}
}
System.out.println(sysFiles);
}
}
class SysFile {
private String filesystem;
private String size;
private String used;
private String avail;
private String usePercentage;
private String mountedOn;
public String getFilesystem() {
return filesystem;
}
public void setFilesystem(String filesystem) {
this.filesystem = filesystem;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getUsed() {
return used;
}
public void setUsed(String used) {
this.used = used;
}
public String getAvail() {
return avail;
}
public void setAvail(String avail) {
this.avail = avail;
}
public String getUsePercentage() {
return usePercentage;
}
public void setUsePercentage(String usePercentage) {
this.usePercentage = usePercentage;
}
public String getMountedOn() {
return mountedOn;
}
public void setMountedOn(String mountedOn) {
this.mountedOn = mountedOn;
}
@Override
public String toString() {
return "SysFile{" +
"filesystem='" + filesystem + '\'' +
", size='" + size + '\'' +
", used='" + used + '\'' +
", avail='" + avail + '\'' +
", usePercentage='" + usePercentage + '\'' +
", mountedOn='" + mountedOn + '\'' +
'}';
}
}
```
输出结果为:
```
[SysFile{filesystem='devtmpfs', size='926M', used='0', avail='926M', usePercentage='0%', mountedOn='/dev'}, SysFile{filesystem='tmpfs', size='944M', used='0', avail='944M', usePercentage='0%', mountedOn='/dev/shm'}, SysFile{filesystem='tmpfs', size='944M', used='440K', avail='944M', usePercentage='1%', mountedOn='/run'}, SysFile{filesystem='tmpfs', size='944M', used='0', avail='944M', usePercentage='0%', mountedOn='/sys/fs/cgroup'}, SysFile{filesystem='/dev/vda1', size='40G', used='2.9G', avail='35G', usePercentage='8%', mountedOn='/'}, SysFile{filesystem='tmpfs', size='189M', used='0', avail='189M', usePercentage='0%', mountedOn='/run/user/0'}]
```
阅读全文