Java——文档监控

要求:

实时监控student.txt文件
如果student.txt发生变化,
把变化的时间及变化的内容记录下来
比如:student_log.txt

代码实现:

一:记录文件发生变化时间

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Record extends Thread {
    private String extime;
    Boolean isok;
    public Record(Boolean isok){
        this.isok=isok;
    }
    public Boolean contral() throws IOException {
        String cpath="D:/文件监控/项目/contral.txt";
        FileReader fr=new FileReader(cpath);
        BufferedReader br=new BufferedReader(fr);
        Boolean b= Boolean.valueOf(br.readLine());
        br.close();
        fr.close();
        return b;
    }
    public void time()  {
        String path="D:/文件监控/项目/student.txt";
        File file=new File(path);
        Date date = new Date(file.lastModified());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(date);
        this.extime=time;
        String newpath="D:/文件监控/项目/student_log.txt";
        FileWriter fw;
        BufferedWriter bw;
        while (true){
            try {
                isok = this.contral();
//                System.out.println(isok);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(!isok){
                System.out.println("over.......");
                break;
            }
            time=df.format(new Date(file.lastModified()));
            if(!extime.equals(time)){
                System.out.println(time);
                this.extime=time;
                try {
                    fw = new FileWriter(newpath,true);
                    bw=new BufferedWriter(fw);
                    bw.write(time);
                    bw.newLine();
                    bw.flush();
                    fw.close();
                    this.sleep(1000);
                } catch (IOException e) {
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public void run() {
        Record r=new Record(true);
        r.time();
    }
}

二、记录文件更改内容

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Compare extends Thread {
    Boolean isok;
    public Compare(Boolean isok){
        this.isok=isok;
    }
    public List read(String path) throws IOException {
        List<Student> list = new ArrayList();
        FileReader fr = new FileReader(path);
        BufferedReader br = new BufferedReader(fr);
        String str = br.readLine();
        while (str != null) {
            Student stu = new Student();
            String[] s = str.split(" ");
            stu.setId(Integer.parseInt(s[0]));
            stu.setName(s[1]);
            stu.setAge(Integer.parseInt(s[2]));
            stu.setSex(s[3]);
            stu.setGrade(Integer.parseInt(s[4]));
            stu.setTel(Integer.parseInt(s[5]));
            list.add(stu);
            str = br.readLine();
        }
        br.close();
        fr.close();
        return list;
    }
    public void copy(String path,String copypath) throws IOException {
        File file=new File(copypath);
        file.createNewFile();
        FileReader fr=new FileReader(path);
        BufferedReader br=new BufferedReader(fr);
        FileWriter fw=new FileWriter(copypath);
        BufferedWriter bw=new BufferedWriter(fw);
        String line=br.readLine();
        while (line!=null){
//            System.out.println(line);
            bw.write(line);
            line=br.readLine();
            if(line!=null){
                bw.newLine();
            }
        }
        bw.flush();
        fw.close();
        br.close();
        fr.close();
    }

    public void com(String path, String copypath,String newpath) throws IOException {
        List<Student> list=this.read(path);
        List<Student> copylist=this.read(copypath);
        FileWriter fw=new FileWriter(newpath,true);
        BufferedWriter bw=new BufferedWriter(fw);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() != copylist.get(i).getId()) {
                System.out.println(list.get(i).getId());
                bw.write(list.get(i).getId());
                bw.newLine();
            }
//            System.out.println(!list.get(i).getName().equals(copylist.get(i).getName()));
            else if (!list.get(i).getName().equals(copylist.get(i).getName())) {
                System.out.println(list.get(i).getName());
                bw.write(list.get(i).getName());
                bw.newLine();
            }
            else if (list.get(i).getAge() != copylist.get(i).getAge()) {
                System.out.println(list.get(i).getAge());
                bw.write(list.get(i).getAge());
                bw.newLine();
            }
            else if (!list.get(i).getSex().equals(copylist.get(i).getSex())) {
                System.out.println(list.get(i).getSex());
                bw.write(list.get(i).getSex());
                bw.newLine();
            }
            else if (list.get(i).getGrade() != copylist.get(i).getGrade()) {
                System.out.println(list.get(i).getGrade());
                bw.write((int) list.get(i).getGrade());
                bw.newLine();
            }
            else if (list.get(i).getTel() != copylist.get(i).getTel()) {
                System.out.println(list.get(i).getTel());
                bw.write(list.get(i).getTel());
                bw.newLine();
            }
        }
        bw.flush();
        fw.close();
    }
    public Boolean contral() throws IOException {
        String cpath="D:/文件监控/项目/contral.txt";
        FileReader fr=new FileReader(cpath);
        BufferedReader br=new BufferedReader(fr);
        Boolean b= Boolean.valueOf(br.readLine());
        br.close();
        fr.close();
        return b;
    }
    @Override
    public void run() {
        String path = "D:/文件监控/项目/student.txt";
        String copypath = "D:/文件监控/项目/copyStudent.txt";
        String newpath="D:/文件监控/项目/student_log.txt";
        try {
            this.copy(path,copypath);

        } catch (IOException e) {
            e.printStackTrace();
        }
        while (true){
            try {
                isok=this.contral();
//                System.out.println(isok);
                if(!isok){
                    System.out.println("over.......");
                    break;
                }
                this.sleep(1000);
                this.com(path,copypath,newpath);
                this.copy(path,copypath);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

 三、学生信息封装

public class Student {
    private int id;
    private String name;
    private int age;
    private String sex;
    private float grade;
    private int tel;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public float getGrade() {
        return grade;
    }

    public void setGrade(float grade) {
        this.grade = grade;
    }

    public int getTel() {
        return tel;
    }

    public void setTel(int tel) {
        this.tel = tel;
    }
}

四、测试类

启动多线程,开启监控

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Test {

    public static void main(String[] args) throws IOException {
        Record r=new Record(true);
        Compare c=new Compare(true);
        r.start();
        c.start();

    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值