java定时读取文件

在项目中经常会用到定时器,在笔试或者面试中也会经常问到定时器和IO流。

复制代码
public class TimerDemo {
    public static void main(String[] args) throws Exception {
        
        Calendar date = Calendar.getInstance();
        //设置固定开始时间为 00:00:00
        date.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DATE), 0, 0, 0);
        long daymin = 5000;//5秒
        long daySpan = 24 * 60 * 60 * 1000;//一天的秒数,使用这个秒数就能在某天的固定时刻触发定时器
        //得到定时器实例
        Timer time = new Timer();
        time.schedule(new TimerTask() {
            public void run() {
                //run中填写定时器主要执行的代码块
                //打印当前时间
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                String date1 = df.format(new Date());// new Date()为获取当前系统时间,也可使用当前时间戳
                System.err.println(date1);
                System.out.println("定时器执行..");
                //1,字符流读取文件
                try {
                    FileReader fr = new FileReader("E:\\demo.txt");
                    BufferedReader br = new BufferedReader(fr);
                    StringBuilder strb = new StringBuilder();
                    while (true) {
                        String line = null;
                        try {
                            line = br.readLine();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        if (line == null) {
                         break;
                        }
                        strb.append(line);
                        String result = strb.toString();
                        System.err.println(result);
                       }
                    
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                //2,字节流读取文件
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream("E:\\demo1.txt");
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                byte[] b = new byte[1024];
                int len = 0;
                try {
                    while((len=fis.read(b))!=-1){
                        System.out.println(new String(b, 0, len));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }, date.getTime(), daymin); //date.getTime()为上面赋值的00:00:00,daymin是执行间隔 
  }; 
}
复制代码

 

这里主要的代码块为:

Timer time = new Timer();
time.schedule(new TimerTask() {
  public void run() {
  //run中填写定时器主要执行的代码块
  }, date.getTime(), daymin); //date.getTime(),为开始时间,这里获取的是上面赋值的时间;daymin为时间间隔
};

run方法中写入自己的代码,我这里主要是用两种方法实现对文件的读取。

控制台打印如上,可以看到每5秒执行一次。

  

posted @   怡安  阅读(625)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示