try with resource_try with resource bufferedwriter

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)));
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值