Java UI界面/实用程序/模拟键盘/模拟输入/文本分析/日志分析

1.文本分析工具

由于曾经在网络安全上有过学习,所以对日志的查看尤为重要

怎样在大批的数据中筛选出自己想要的数据呢,我搜索了好多工具,很难满足自己特定的需求

于是我决定自己编写一个程序,来筛选

这是一个大小只有10KB的程序,但是功能却不小

与其他同类成熟的工具的相比,有以下优点:

1:可以批量分析文本,只需要把所有文件集中到一个文件夹中即可

2:支持1000个自定义规则的保存,在再次启动时无需重新输入,只需要加载好已经保存的规则即可

3:具有强大的异常处理机制,具有状态显示功能

4:具备筛选无穷多字符串的功能,只需要以回车符分开即可

其不仅仅能分析日志,也能分析常见的文本,保留有用的内容

 

 

 当然,还有很多的不足,功能并不是特别的强大

1:只能在安装有jre运行环境的机器上使用

2:不支持字符串模糊匹配的功能

3:功能单一,如果能加入其他功能就更好了,比如替换所有文件的指定字符串

但是仅仅这些我就做了6个小时,尤其是IO的操作,大量的异常随时出现,经过不断修改,已经成为非常稳定的程序了

以后有时间的话我会定期去更新,加入更强大的功能,如果有想法的话可以私聊或评论,我会做一些更实用的工具https://study.gengronglin.top/Source/An.zip已发布至服务器,可以免费下载

由于网站采用自签名SSL证书,访问可能会提示证书无效或者过期,提示危险,不需要担心,放心下载即可

package Analysis;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Arrays;

public class LogAnalysisGUI implements SendClass{
    Rule r=new Rule();
    LogAnalysisGUI() throws IOException {
        JFrames t=new JFrames(this);
    }
    @Override
    public void Do() throws FileNotFoundException {
        int i=0,c=0;
        for(String Name:Select(new File(r.SelectPath))) {
            StringBuilder St = new StringBuilder();
            if (Name != null) {
                FileInputStream fis = new FileInputStream(r.SelectPath + "\" + Name);
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                    while (true) {
                        try {
                            String s = br.readLine();
                            if (!s.equals("") && check(s)) {
                                St.append(s);
                                St.append("n");
                            }
                        }
                        catch (Exception e){
                            break;
                        }
                    }
                if (!String.valueOf(St).equals("")) {
                    FileOutputStream fos = new FileOutputStream(r.SelectPath + "\" + Name.substring(0, Name.lastIndexOf(".")) + r.LastName);
                    PrintWriter pw = new PrintWriter(fos);
                    pw.write(String.valueOf(St));
                    pw.close();
                    c++;
                }
                i++;
            }
        }
        JPanels.Message.append("执行成功!共处理了"+i+"个文件,生成了"+c+"个文件n");
    }
    public static void main(String[] args) throws IOException {
        new LogAnalysisGUI();
    }

    public boolean check(String s) {
        for (String a : r.Must) {
            try {
                if (!s.contains(a)) {
                    return false;
                }
            }
            catch (Exception e){
                JPanels.Message.append(Arrays.toString(e.getStackTrace()) +"A56n");
            }
        }
        for (String a : r.MustNot) {
            if (!a.equals("")&&s.contains(a)) {
                return false;
            }
        }
        return true;
    }

    public String[] Select(File file) {
        String T[]=new String[1000];
        int i=0;
        File[] files = file.listFiles();
        assert files != null;
        for (File fi : files) {
                String s =fi.getName();
                if(s.contains(r.SelectFile)){
                    T[i]=s;
                    i++;
                    System.out.println(s);
                }
        }
        return T;
    }

    @Override
    public void Send(Rule r) {
        this.r=r;
    }
}
class JFrames extends JFrame{
    JPanels jp;
    JFrames(SendClass sc) throws IOException {
        jp=new JPanels(sc);
        setTitle("日志分析工具");
        setSize(750,750);
        setResizable(false);
        setDefaultCloseOperation(JFrames.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        add(jp);
        setVisible(true);
    }
}
class JPanels extends JPanel implements ActionListener {
    SendClass sc;
    JLabel l1=new JLabel("指定路径+文件夹名,绝对路径或者相对路径:");
    JLabel l2=new JLabel("文件夹内日志文件名筛选规则(必须包含的字符串,不能非空!):");
    JLabel l3=new JLabel("重命名之后的日志扩展名(不要与原文件扩展名相同):");
    JLabel l4=new JLabel("内容行筛选规则(必须包含的字符串)以回车符分割多个字符串:");
    JLabel l5=new JLabel("内容行筛选(必须不包含的字符串)以回车符分割多个字符串:");
    JScrollPane jsp1=new JScrollPane();
    JScrollPane jsp2=new JScrollPane();
    JScrollPane jsp3=new JScrollPane();
    JTextField t1=new JTextField("Test");
    JTextField t2=new JTextField(".log");
    JTextField t3=new JTextField(".xml");
    JTextArea ta1=new JTextArea("");
    JTextArea ta2=new JTextArea("");
    JButton b1=new JButton("分析并保存文件在同一文件夹");
    JButton b2=new JButton("保存筛选规则");
    JTextField tx=new JTextField("规则1");
    JButton b3=new JButton("加载筛选规则");
    JComboBox jb=new JComboBox();
    static JTextArea Message=new JTextArea("启动成功!n");
    JPanels(SendClass sc){
        this.sc=sc;
        setLayout(null);
        l1.setBounds(0,0,300,20);
        t1.setBounds(0,20,300,20);
        l2.setBounds(0,40,300,20);
        t2.setBounds(0,60,300,20);
        l3.setBounds(0,80,300,20);
        t3.setBounds(0,100,300,20);
        l4.setBounds(320,0,430,20);
        l5.setBounds(320,350,430,20);
        b1.setBounds(0,120,300,40);
        tx.setBounds(0,170,300,20);
        b2.setBounds(0,200,300,40);
        jb.setBounds(0,250,300,30);
        b3.setBounds(0,290,300,40);
        jsp1.setBounds(340,20,380,330);
        jsp2.setBounds(340,370,380,330);
        jsp3.setBounds(10,350,290,300);
        jsp1.getViewport().add(ta1);
        jsp2.getViewport().add(ta2);
        jsp3.getViewport().add(Message);
        add(l1);
        add(l2);
        add(l3);
        add(l4);
        add(l5);
        add(t1);
        add(t2);
        add(t3);
        add(b1);
        add(jsp1);
        add(jsp2);
        add(jsp3);
        add(tx);
        add(b2);
        add(jb);
        add(b3);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        try {
            Load();
        }
        catch (Exception e){
            Message.append(Arrays.toString(e.getStackTrace()) +"A167n");//167
        }


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==b1){
            try {
                String Must=ta1.getText();
                String MustNot=ta2.getText();
                String MustContain=t2.getText();
                String Path=t1.getText();
                String Name=t3.getText();
                String RuleName=tx.getText();
                Rule newRule =new Rule();
                newRule.name=RuleName;
                newRule.Must= Must.split("n");
                newRule.MustNot= MustNot.split("n");
                newRule.SelectFile= MustContain;
                newRule.SelectPath= Path;
                newRule.LastName=Name;
                sc.Send(newRule);
                sc.Do();
            } catch (FileNotFoundException fileNotFoundException) {
                fileNotFoundException.printStackTrace();
            }
        }
        else if(e.getSource()==b2){
            String Must=ta1.getText();
            String MustNot=ta2.getText();
            String MustContain=t2.getText();
            String Path=t1.getText();
            String Name=t3.getText();
            String RuleName=tx.getText();
            Rule newRule =new Rule();
            newRule.name=RuleName;
            newRule.Must= Must.split("n");
            newRule.MustNot= MustNot.split("n");
            newRule.SelectFile= MustContain;
            newRule.SelectPath= Path;
            newRule.LastName=Name;
            sc.Send(newRule);
            if(checkError(newRule)){
                try {
                    SaveRule(newRule);
                    Message.append("保存成功!n");
                } catch (Exception ex) {
                    Message.append(Arrays.toString(ex.getStackTrace()) +"A200n");//200
                }
            }else{
                Message.append("名称或者路径不能为空");
            }
        }
        else if(e.getSource()==b3){
            try {
                Rule s=LoadRule(jb.getSelectedItem().toString());
                sc.Send(s);
                ta1.setText("");
                for(int i=0;i<s.Must.length;i++){
                    ta1.append(s.Must[i]);
                    if(i!=s.Must.length-1){ta1.append("n");}
                }
                ta2.setText("");
                for(int i=0;i<s.MustNot.length;i++){
                    ta2.append(s.MustNot[i]);
                    if(i!=s.MustNot.length-1){ta2.append("n");}
                }
                t1.setText(s.SelectPath);
                t2.setText(s.SelectFile);
                t3.setText(s.LastName);
                tx.setText(s.name);
                Message.append("加载成功!n");
            } catch (Exception ex) {
                Message.append(Arrays.toString(ex.getStackTrace()) +"A223n");//223
            }
        }
    }

    private boolean checkError(Rule newRule) {
        if(newRule.SelectPath.equals("")){return false;}
        return true;
    }

    public void SaveRule(Rule r) throws IOException{
        Rule []A=new Rule[1000];
        File f=new File("Rule.sav");
        if(!f.exists()){
            f.createNewFile();
            FileOutputStream fos=new FileOutputStream(f);
            ObjectOutputStream oos=new ObjectOutputStream(fos);
            oos.writeObject(A);
        }
        FileInputStream fis=new FileInputStream(f);
        ObjectInputStream ois=null;
        try{
            ois=new ObjectInputStream(fis);
        }
        catch (Exception ex){
            Message.append(Arrays.toString(ex.getStackTrace()) +"A246n");//246
        }
        try {
            A= (Rule[]) ois.readObject();
        }
        catch (Exception e){
            Message.append(Arrays.toString(e.getStackTrace()) +"A252n");//252
        }
        for(int i=0;i<1000;i++){
            if(A[i] == null || A[i].name.equals(r.name)){
                A[i]=r;
                break;
            }
        }
        FileOutputStream fos=new FileOutputStream(f);
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        oos.writeObject(A);
        Load();
    }

    public Rule LoadRule(String Name) throws IOException{
        File f=new File("Rule.sav");
        if(!f.exists()){
            f.createNewFile();
            Rule []A=new Rule[1000];
            FileOutputStream fos=new FileOutputStream(f);
            ObjectOutputStream oos=new ObjectOutputStream(fos);
            oos.writeObject(A);
        }
        FileInputStream fis=new FileInputStream(f);
        ObjectInputStream ois=null;
        try{
            ois=new ObjectInputStream(fis);
        }
        catch (Exception ex){
            Message.append(Arrays.toString(ex.getStackTrace()) +"A277n");//277
        }
        Rule []A=new Rule[1000];
        try {
            A= (Rule[]) ois.readObject();
        }
        catch (Exception e){
            Message.append(Arrays.toString(e.getStackTrace()) +"A284n");//284
        }
        for(Rule x:A){
            if(x.name.equals(Name)){
                return x;
            }
        }
        return null;
    }


    public void Load() throws IOException{
        File f=new File("Rule.sav");
        if(!f.exists()){
            f.createNewFile();
            Rule []A=new Rule[1000];
            FileOutputStream fos=new FileOutputStream(f);
            ObjectOutputStream oos=new ObjectOutputStream(fos);
            oos.writeObject(A);
        }
        FileInputStream fis=new FileInputStream(f);
        ObjectInputStream ois=null;
        try{
            ois=new ObjectInputStream(fis);
        }
        catch (Exception ex){
            Message.append(Arrays.toString(ex.getStackTrace()) +"A306n");//306
        }
        Rule []A=new Rule[1000];
        try {
                A = (Rule[]) ois.readObject();
        }
        catch (Exception e){
            Message.append(Arrays.toString(e.getStackTrace()) +"A313n");//313
        }
        jb.removeAllItems();
        for(Rule x:A){
            if(x!=null) {
                jb.addItem(x.name);
            }
        }
    }
}
class Rule implements Serializable{
    String name="";
    String[] Must = {};
    String[] MustNot = {};
    String SelectFile="";
    String SelectPath="";
    String LastName="";
}
interface SendClass{
    void Send(Rule r);
    void Do() throws FileNotFoundException;
}

可以参考参考代码,如果喜欢,请支持原创,并给予鼓励,包括源码,程序,相关文件仅供个人使用,请勿用于其他用途!

2.模拟键盘输入工具

事实上,这种工具现在有很多很多,甚至自己如果有编程基础的话,随随便便就能用vbs脚本编写一个很简单的程序

我编写这个程序的目的呢,

其一是为了学习Java Robot自动化类,以便能够开发更多实用的工具

其二是想做一个比较专业一点的自动点击工具,比如vbs脚本不能够随时终止,曾经由于输出大量的回车符,导致电脑彻底卡死,所以我就用了java开发

特点:启用了3个线程,使得既能同时运行程序,又能随时控制程序的终止,又能随时查看程序执行进度

也能够方便的大量发送信息(哈哈,不建议,可能会被冻结,不要问我怎么知道的)

 

 

 由于代码太多,所以这里就不暂时了,想了解的话直接去我服务器网站上下载压缩包即可,里面包含了jar可执行程序,java源码,class编译后的程序,以及简单教程

https://study.gengronglin.top/Source/Au.zip

由于网站采用自签名SSL证书,访问可能会提示证书无效或者过期,提示危险,不需要担心,放心下载即可

可以参考参考代码,如果喜欢,请支持原创,并给予鼓励,包括源码,程序,相关文件仅供个人使用,请勿用于其他用途!

如果喜欢,请打赏,如果不愿意,下载使用即可,如果有想法可以评论或者私聊

以便激励我能够做更多的程序

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>