JAVA学习笔记20210420_监听文件打开操作,设置密码

监听文件夹变化

ref:Interface FileAlterationListener

变化监听类:FileListener.java

/**
* this is a file listener class
* @author XYM_
* @date 2021-4-16
* @version1.0
*/
import java.io.File;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;

public class FileListener implements FileAlterationListener{
    /**
    * rewrite the methods
    * @author XYM_
    * @param fao FileAlterationObserver
    * @param dir file directory
    * @param f file
    * @authoe XYM_
    * @version 1.0
    */
    @Override
    public void onStart(FileAlterationObserver fao){
       System.out.println("start...");
    }

    @Override
    public void onStop(FileAlterationObserver fao){
       System.out.println("stop...");
    }

    @Override
    public void onDirectoryCreate(File dir){
        System.out.println("the folder"+dir.getAbsolutePath()+"is created");
    }

    @Override
    public void onDirectoryChange(File dir){
        System.out.println("the folder"+dir.getAbsolutePath()+"is changed");
    }

    @Override
    public void onDirectoryDelete(File dir){
        System.out.println("the folder"+dir.getAbsolutePath()+"is deleted");
    }

    @Override
    public void onFileCreate(File f){
        System.out.println("the file"+f.getAbsolutePath()+"is created");
    }

    @Override
    public void onFileChange(File f){
        System.out.println("the file"+f.getAbsolutePath()+"is changed");
    }

    @Override
    public void onFileDelete(File f){
        System.out.println("the file"+f.getAbsolutePath()+"is deleted");
    }
}

测试类FileMonitor.java:

/**
* this is a file monitor test
* @author XYM_
* @date 2021-4-16
* @version 1.0
*/
import java.io.File;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.apache.commons.io.monitor.FileAlterationMonitor;

public class FileMonitor{
    FileAlterationMonitor fam = null;
    /**
    * this is a constructor of file monitor
    */
    public FileMonitor(long interval){
        fam = new FileAlterationMonitor(interval);
    }
    /**
    * add monitor method
    * @param file
    * @param listener
    * @author XYM_
    * @version 1.0
    */
    public void monitor(File file, FileListener listener){
        FileAlterationObserver ob = new FileAlterationObserver(file);
        ob.addListener(listener);
        fam.addObserver(ob);
    }

    public void stop() throws Exception{
        fam.stop();
    }
    public void start() throws Exception{
        fam.start();
    }

    public static void main(String[] args){
        try{
        // define a new monitor with the interval 5000ms
        FileMonitor fm = new FileMonitor(5000);
        FileListener fl = new FileListener();
        File f = new File("D:/PractiseJava/Monitor/MoniTest");
        fm.monitor(f, fl);
        fm.start();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

运行中进行复制文件进入文件夹操作,打印结果:

D:\PractiseJava\Monitor>javac -cp commons-io-2.8.0.jar FileListener.java FileMonitor.java

D:\PractiseJava\Monitor>java -classpath commons-io-2.8.0.jar; FileMonitor
start...
the folderD:\PractiseJava\Monitor\MoniTest\subFileis changed
the fileD:\PractiseJava\Monitor\MoniTest\subFile\Pump.classis created
stop...

遇见的问题:需要额外加载apache commons-io的jar包,下载要用的jar包放在当前目录下,编译和运行的时候加载。

设置窗体监听鼠标操作

ref:Java监听接口深入理解

窗体设置和测试代码JFrame.java:

/**
* this is an event frame set class
* @author XYM_
* @date 2021-4-16
* @version 1.0
*/

public class JFrame{
    /**
    * method to show ui
    * @author XYM_
    * @version 1.0
    */
    public void showFrame(){
        javax.swing.JFrame frame = new javax.swing.JFrame();
        frame.setTitle("Secret File");
        java.awt.FlowLayout f = new  java.awt.FlowLayout();
        frame.setLayout(f);
        javax.swing.JLabel la = new javax.swing.JLabel("Key");
        javax.swing.JTextField te = new javax.swing.JTextField(8);
        javax.swing.JButton bu = new javax.swing.JButton("OK");
        frame.add(la);
        frame.add(te);
        frame.add(bu);
        frame.setSize(300,100);
        frame.setDefaultCloseOperation(3);
        frame.setVisible(true);

        Listener ls = new Listener();
        //add the listener to the button
        bu.addActionListener(ls);
        //get the input of text
        ls.setKey(te);
        ls.setFrame(frame);
    }
    public static void main(String[] args){
        JFrame needKey = new JFrame();
        needKey.showFrame();
    }
}

鼠标操作监听类Listener.java:

/**
* this is an action listener class implements the interface ActionListener
* @author XYM_
* @date 2021-4-16
* @version 1.0
*/

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JFrame;

public class Listener implements ActionListener{
    JTextField key;
    JFrame frame;
    /**
    * method to get the input of text
    * @param key
    * @author XYM_
    * @version 1.0
    */
    public void setKey(JTextField key){
        this.key = key;
    }
    /**
    * method to get the frame
    * @param key
    * @author XYM_
    * @version 1.0
    */
    public void setFrame(JFrame frame){
        this.frame = frame;
    }
    /**
    * method to deal with the action
    * @param e action event
    * @author XYM_
    * @version 1.0
    */
    public void actionPerformed(ActionEvent e){
        String str = key.getText();
        System.out.println(str);
        if(str.equals("123")){
            System.out.println("password correct");
            frame.dispose();
        }else{
            key.setText("WrongKey");
        }
    }
}

运行结果:
控制台可以监控尝试过的密码,正确后返回信息并关闭窗口:

D:\PractiseJava\Monitor>javac Listener.java JFrame.java

D:\PractiseJava\Monitor>java JFrame
456
xsne
123
password correct
文件打开操作的监听

ref:
在Windows上监听“用我的java应用程序打开文件”事件
Class StartupNotification

使用StartupNotification类的问题:
该类在包com.install4j.api.launcher中,从参考链接的API Overview中知道使用该包需要有install4j,

Classpath for framework classes
When developing your own classes, you have to add the install4j API to your compile classpath.
Do not distribute this jar file with your application, install4j will handle this for you. i4jruntime.jar will be automatically available on the classpath in the installer as well as for all launchers generated by install4j.
The install4j runtime is available as a Maven dependency…

使用命令行编译的情况下需要引用i4jruntime的jar包,下载install4j,在安装路径下找到i4jruntime.jar,编译时引用classpath,仍失败,显示程序包com.install4j.api中找不到launcher,因此也找不到StartupNotification类。

以下代码没有编译成功:

/**
* this is a file listener for the open action
* @author XYM_
* @date 2021-4-20
* @version 1.0
*/

import com.install4j.api.launcher;
import java.util.EventListener;
import java.io.File;
import java.lang.Object;

public class FListener implements StartupNotification.Listener{
    @Override
    public void startupPerformed(java.lang.String dir){
        System.out.println("the folder" + dir + "is being accessed");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值