当前位置:网站首页>try with resource
try with resource
2022-06-25 16:38:00 【四问四不知】
序言
最近需要统计一个日志里的某个字段信息,日志格式如下,需要统计有规律的一些日志记录,手动统计太过麻烦,所以自己写一个读取log文件并统计一下总数和平均每秒告警数,代码如下。
2021-06-17 19:05:23.865 ...
2021-06-17 19:05:24.869 ...
2021-06-17 19:05:24.860 ...
2021-06-17 19:05:23.898 alarm to list for xxx: ...
2021-06-17 19:05:23.985 alarm to list for xxx: ...
2021-06-17 19:05:23.999 alarm to list for xxx: ...
......(省略,其实每行日志都会有时间格式数据)
2021-06-17 19:06:45.125 alarm to list for xxx: ...
package com.hust.zhang.trywithresource;
import java.io.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
public class FileHandler {
private static final String inputFilePath = "/Users/kaizhang/test/input.log";
private static final String outputFilePath = "/Users/kaizhang/test/output.txt";
private static final String fixedSubString = "alarm to list for xxx";
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader(new File(inputFilePath)));
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(outputFilePath)))) {
String line;
int count = 0;
String startTime = "";
String endTime="";
while ((line = reader.readLine()) != null) {
if(Objects.equals(count, Integer.valueOf(0))){
startTime = line.substring(0, 23);
}
if (line.contains(fixedSubString)){
count ++;
endTime = line.substring(0, 23);
}
}
int res = average(startTime, endTime, count);
writer.write("每秒统计个数为:"+res);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 求平均每秒的告警个数
* @param startTime
* @param endTime
* @param count
* @return
*/
private static int average(String startTime, String endTime, int count) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime startDate = LocalDateTime.parse(startTime, formatter);
LocalDateTime endDate = LocalDateTime.parse(endTime, formatter);
Duration period = Duration.between(startDate, endDate);
long second = period.getSeconds();
return count/(int)second;
}
}
主要是日志数据过多,人工统计起来太耗时,只要是有规律的数据,我们就能够想到办法去偷懒去统计。不过上面用到try with resource这种语法糖来简化代码,省去了释放IO流的过程,如果不这样,我们通常会通过try-catch-finally语句块在最后释放资源。下面简单介绍一下try with resource。
介绍
try-with-resources 是 JDK 7 中一个新的异常处理机制,它能够很容易地关闭在 try-catch 语句块中使用的资源。所谓的资源(resource)是指在程序完成后,必须关闭的对象。try-with-resources 语句确保了每个资源在语句结束时关闭。所有实现了 java.lang.AutoCloseable 接口(其中,它包括实现了 java.io.Closeable 的所有对象),可以使用作为资源。
实战
写一个简单的例子,先写一个Connection类实现AutoCloseable接口,如下,
public class Connection implements AutoCloseable {
public void sendData() {
System.out.println("send data ......");
}
@Override
public void close() throws Exception {
System.out.println("close connection ......");
}
}
再写测试类TryWithResource
public class TryWithResource {
public static void main(String[] args) {
try (Connection con = new Connection()) {
con.sendData();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行一下,结果如下,你会发现使用try-with-resource语法糖后,发送完数据后,它会自动执行AutoCloseable接口的close()方法,这样写不仅代码更加简洁,且不用担心自己忘记释放IO资源导致浪费系统资源(包括buffer、File descriptor table、Open file table、I-node table等资源,这些是不会被JVM的垃圾回收机制所回收的系统资源)
版本演进
try-with-resources 既然是 JDK 7 中一个新的异常处理机制,那我们看一下从JDK 7开始它有哪些变化,毕竟知道了来龙去脉来能够更好的使用它。
Java SE 7 规范
比如我们写一个读取文件内容的方法,
package com.hust.zhang.trywithresource;
import javax.validation.constraints.NotNull;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ReadDate {
public static void main(String[] args) throws IOException {
String inputFilePath = "/Users/kaizhang/test/input.log";
List<String> data1 = readData(inputFilePath);
data1.stream().forEach(System.out::println);
}
private static List<String> readData(@NotNull String inputPath) throws FileNotFoundException {
List<String> strings = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(new File(inputPath)));
try (BufferedReader br = reader) {
for (;;) {
String line = br.readLine();
if (line == null)
break;
strings.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return strings;
}
}
Java SE 9 规范
在Java SE 7中,如果有一个资源是final或等效于final变量,那么在在 try 语句块中声明资源 br,然后才能使用try-with-resource外声明的资源变量。在Java SE 9 中无需在 try-with-resources 语句中声明一个新变量。
package com.hust.zhang.trywithresource;
import javax.validation.constraints.NotNull;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ReadDate {
public static void main(String[] args) throws IOException {
String inputFilePath = "/Users/kaizhang/test/input.log";
List<String> data1 = readData(inputFilePath);
data1.stream().forEach(System.out::println);
}
private static List<String> readData(@NotNull String inputPath) throws FileNotFoundException {
List<String> strings = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(new File(inputPath)));
try (reader) {
for (;;) {
String line = br.readLine();
if (line == null)
break;
strings.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return strings;
}
}
其实看到只有比较微小的变化,就是在try后面不需要重新声明资源再使用它。现在已经到了JDK 17版本了,后面有什么变化就由大家自己去探索了。
总结
像上面从JDK 7 到JDK 9 其实变化并不算大,而且平时我们使用IO资源时一般直接是在try-with-resource语句中声明。而且后面JDK 8 引入了Stream流的概念,加上Files类等,大大简化了读取IO流的动作,可以看到Files.readAllLines()方法里也用到了try-with-resource这种语法,使用一些工具类去完成所需的功能让代码可读性变强且更加优雅。
package com.hust.zhang.trywithresource;
import javax.validation.constraints.NotNull;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ReadDate {
public static void main(String[] args) throws IOException {
String inputFilePath = "/Users/kaizhang/test/input.log";
List<String> data = readData(inputFilePath);
data.stream().forEach(System.out::println);
}
private static List<String> readData(@NotNull String inputPath) throws IOException {
Path path = Paths.get(inputPath);
Stream<String> lines = Files.readAllLines(path).stream().filter(line->!line.isEmpty());
return lines.collect(Collectors.toList());
}
}
参考链接:
1、https://www.oracle.com/technical-resources/articles/java/trywithresources.html
边栏推荐
- 【NLP】今年英语高考,CMU用重构预训练交出134高分,大幅超越GPT3
- XXIX - orbslam2 real-time 3D reconstruction using realsensed435
- Reading mysql45 lecture - index continued
- 效应与定律
- App测试工具大全,收藏这篇就够了
- 3.条件概率与独立性
- 剑指 Offer 39. 数组中出现次数超过一半的数字
- [proficient in high concurrency] deeply understand the basis of C language and C language under assembly
- DDD概念复杂难懂,实际落地如何设计代码实现模型?
- mysql使用过程中遇到的问题
猜你喜欢
vscode插件自用
Kettle表输入组件精度丢失的问题
Day_ ten
Day_ seventeen
Involution? Foam? Change? Ten questions directly hit the core puzzle of "meta universe" – the essence of "ask ta- Wang Lei about the time of the universe"
This latest research has revealed two secrets of cloudy development
Sword finger offer 50 First character that appears only once
SDN系统方法 | 10. SDN的未来
Kalman time series prediction
How did I raise my salary to 20k in three years?
随机推荐
redis 分布式锁整理
Day_ ten
一个 TDD 示例
[Jianzhi offer II 091. painting the house]
pychrm的这些配置,你都知道吗?
Day_ 16 set
Why does MySQL limit affect performance?
【蓝桥杯集训100题】scratch指令移动 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第14题
Involution? Foam? Change? Ten questions directly hit the core puzzle of "meta universe" – the essence of "ask ta- Wang Lei about the time of the universe"
IO stream
Unity技术手册 - 干扰/噪音/杂波(Noise)子模块
1-8Vmware中的文件共享
二十九-使用RealSenseD435进行ORBSLAM2实时三维重建
Redis系列——概述day1-1
使用hbuilder X创建uniapp项目
Xshell connecting VMware virtual machines
The problem of missing precision of kettle table input components
How smart PLC constructs ALT instruction
JVM内存结构
Babbitt yuan universe daily recommendation: three players holding "tens of millions" of collections have revealed the "three routines" of the digital collection market