当前位置:网站首页>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
边栏推荐
- Kalman Filter 遇到 Deep Learning : 卡尔曼滤波和深度学习有关的论文
- 剑指 Offer 50. 第一个只出现一次的字符
- 剑指 Offer II 010. 和为 k 的子数组 前缀和差
- Vscode plug-in self use
- 计网 | 形象理解路由协议RIP、OSPF、BGP
- Why does MySQL limit affect performance?
- 根据先序遍历和中序遍历生成后序遍历
- 揭秘GES超大规模图计算引擎HyG:图切分
- 【蓝桥杯集训100题】scratch指令移动 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第14题
- Android修行手册之Kotlin - 自定义View的几种写法
猜你喜欢

Understanding of reflection part

Day_ ten

Unity技术手册 - 生命周期内大小(Size over Lifetime)和速度决定大小(Size by Speed)

How did I get a salary increase of 13k+ after one year of employment?

剑指 Offer 50. 第一个只出现一次的字符

Redis系列——概述day1-1

Optimization of lazyagg query rewriting in parsing data warehouse

Android修行手册之Kotlin - 自定义View的几种写法

3. conditional probability and independence

Tensorflow old version
随机推荐
internship:Swagger下注解的涉及 接口的提供
微信公众号服务器配置
Unity技术手册 - 干扰/噪音/杂波(Noise)子模块
Redis series - overview day1-1
Wireshark network card cannot be found or does not display the problem
代码注释的艺术,优秀代码真的不需要注释吗?
Are these old system codes written by pigs?
About: encryption and decryption of rsa+aes data transmission [chapter], project practice (special summary)
批量--07---断点重提
Paper notes: generalized random forests
Optimization of lazyagg query rewriting in parsing data warehouse
Redis系列——概述day1-1
计网 | 形象理解路由协议RIP、OSPF、BGP
WPF开发随笔收录-心电图曲线绘制
A TDD example
3年,我是如何涨薪到20k?
【 apprentissage automatique】 cas de prévision et d'analyse de l'examen d'entrée à l'Université basé sur des séries chronologiques multiples
STM32硬件错误HardFault_Handler的处理方法
vscode插件自用
Day_ seventeen