try with resource

序言

最近需要统计一个日志里的某个字段信息,日志格式如下,需要统计有规律的一些日志记录,手动统计太过麻烦,所以自己写一个读取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

2、Java 9 改进的 try-with-resources | 菜鸟教程 (runoob.com)

3、http://tutorials.jenkov.com/java-nio/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值