Java小工具

统计系统磁盘, 不需要其他依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public static void main(String[] args) throws IOException {
DecimalFormat df = new DecimalFormat("#0.00");
long diskSize = 0;
long diskUsed = 0;
File[] disks = File.listRoots();
for (File file : disks) {
// 获取盘符
System.out.print(file.getCanonicalPath() + " ");
// 获取总容量
long totalSpace = file.getTotalSpace();
diskSize += totalSpace;
// 获取剩余容量
long usableSpace = file.getUsableSpace();
// 获取已经使用的容量
long freeSpace = totalSpace - usableSpace;
diskUsed += freeSpace;
// 获取使用率
float useRate = (float)((freeSpace * 1.0 / totalSpace) * 100);
System.out.print("总容量: " + totalSpace / 1024 / 1024 / 1024 + "GB ");
System.out.print("已经使用: " + freeSpace / 1024 / 1024 / 1024 + "GB ");
System.out.print("剩余容量: " + usableSpace / 1024 / 1024 / 1024 + "GB ");
System.out.println("使用率: " + Double.parseDouble(df.format(useRate)) + "% ");
}
float useRate = (float)((diskUsed * 1.0 / diskSize) * 100);
System.out.println("所有磁盘总容量:"+diskSize / 1024 / 1024 / 1024 + "GB ");
System.out.println("所有磁盘已经使用:"+diskUsed / 1024 / 1024 / 1024 + "GB ");
System.out.println("所有磁盘使用率:"+ Double.parseDouble(df.format(useRate))+"%");
System.out.println(new SystemDataController().findAll());
}

统计CPU和内存, 需要用到fastjson2

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.13</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public static void main(String[] args) {
long GB = 1024 * 1024 * 1024;
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
String osJson = JSON.toJSONString(operatingSystemMXBean);
System.out.println("osJson is " + osJson);
JSONObject jsonObject = JSON.parseObject(osJson);
double processCpuLoad = jsonObject.getDouble("processCpuLoad") * 100;
double systemCpuLoad = jsonObject.getDouble("systemCpuLoad") * 100;
Long totalPhysicalMemorySize = jsonObject.getLong("totalPhysicalMemorySize");
Long freePhysicalMemorySize = jsonObject.getLong("freePhysicalMemorySize");
double totalMemory = 1.0 * totalPhysicalMemorySize / GB;
double freeMemory = 1.0 * freePhysicalMemorySize / GB;
double memoryUseRatio = 1.0 * (totalPhysicalMemorySize - freePhysicalMemorySize) / totalPhysicalMemorySize * 100;

StringBuilder result = new StringBuilder();
result.append("系统CPU占用率: ")
.append(twoDecimal(systemCpuLoad))
.append("%,内存占用率:")
.append(twoDecimal(memoryUseRatio))
.append("%,系统总内存:")
.append(twoDecimal(totalMemory))
.append("GB,系统剩余内存:")
.append(twoDecimal(freeMemory))
.append("GB,该进程占用CPU:")
.append(twoDecimal(processCpuLoad))
.append("%");
System.out.println(result.toString());
}
public static double twoDecimal(double doubleValue) {
BigDecimal bigDecimal = new BigDecimal(doubleValue).setScale(2, RoundingMode.HALF_UP);
return bigDecimal.doubleValue();
}

java递归树并分页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* 分页
* @param idList 要查询的条件
* @param pageNum
* @param pageSize
* @return
*/
private PageInfo result(List<Object> idList, int pageNum, int pageSize){
List<BaseMenuVO> result = selectTree("0", menuMapper.findByIds(idList));
List<BaseMenuVO> baseMenuVOS = result
.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
PageInfo pageInfo = new PageInfo(result);
pageInfo.setTotal(result.size());
pageInfo.setList(baseMenuVOS);
pageInfo.setPageNum(pageNum);
pageInfo.setPageSize(pageSize);
return pageInfo;
}

/**
* 生成树
* @param parentId 父级id
* @param treeEntityList 数据源
* @return
*/
private List<BaseMenuVO> selectTree(String parentId, List<BaseMenuVO> treeEntityList) {
List<BaseMenuVO> collect = treeEntityList.stream()
.filter(treeEntity -> treeEntity.getParentId().equals(parentId))
.peek(treeEntity -> treeEntity.setChildren(selectTree(treeEntity.getId(), treeEntityList)))
.collect(Collectors.toList());
return collect;
}

求两个数组的差集、补集

1
2
3
4
5
List<String> deleteList =
list1.stream().filter(e -> !list2.contains(e)).collect(Collectors.toList());

List<String> addList =
list2.stream().filter(e -> !list1.contains(e)).collect(Collectors.toList());