读取日志,将有用信息存入excel

news/2024/7/21 7:28:58 标签: java, excel
 
<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.10</version>
</dependency>

读取日志,将有用信息存入excel

java">public static void main(String[] args){
        String filePath = "C:\\tempJar\\runtime.log";
        String targetFilePath = "C:\\tempJar\\tadiao.xls";
        readTxtFile(filePath, targetFilePath, 1, 1000);
    }

    public static void readTxtFile(String filePath, String targetFilePath, Integer lineBegin, Integer lineMax){
        try {
            File fileA = new File(targetFilePath);
            String encoding="UTF-8";
            File file=new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                int count = lineBegin;
                Workbook wb = Workbook.getWorkbook(fileA); // 获得原始文档
                WritableWorkbook workbookA = Workbook.createWorkbook(fileA, wb);
                WritableSheet sheetA = workbookA.getSheet(0);
                Label label = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    if(!lineTxt.contains("sendLog2Kafka info")){
                        continue;
                    }
                    if(!lineTxt.contains("cu1xfw6x4nefwvG1")){
                        continue;
                    }
                    System.out.println(lineTxt);

                    String splitStr = lineTxt.substring(188);
                    System.out.println(splitStr);
                    JSONObject jSONObject = null;
                    try {
                        jSONObject = JSONObject.parseObject(splitStr);
                    }catch(Exception e){
                        continue;
                    }

                    List<String> columnList = new ArrayList<>();
                    columnList.add("data_type");
                    columnList.add("device_key");
                    columnList.add("device_name");
                    columnList.add("device_position");
                    columnList.add("device_project");
                    columnList.add("forward_info");
                    columnList.add("org_id");
                    columnList.add("product_key");
                    columnList.add("request_id");
                    columnList.add("request_time");

                    int index = 0;
                    for(String column:columnList){
                        String dataTypeValue = (String)jSONObject.get(column);
                        label = new Label(index,count,dataTypeValue);
                        sheetA.addCell(label);
                        index ++;
                    }
                    if(count >= lineMax){
                        break;
                    }

                    count ++;
                }
                workbookA.write();    //写入数据
                workbookA.close();
                read.close();
            }else{
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
        }
    }


http://www.niftyadmin.cn/n/1407998.html

相关文章

wpf只运行一个实例

在winform下&#xff0c;只运行一个实例只需这样就可以&#xff1a;1&#xff0e; 首先要添加如下的namespace&#xff1a; using System.Threading; 2&#xff0e; 修改系统Main函数&#xff0c;大致如下&#xff1a; bool bCreatedNew; //Create a new mutex using…

在Linux上安装Oracle11gR2

实验环境:RHEL7.4 x64 最简安装数据库版本: Oracle 11G R2IP 地址:192.168.10.133Linux连接工具: xmanager 5 //也可以直接使用图形安装Linux无需工具程序包下载:官网下载 Database安装步骤1. 修改系统主机名&#xff0c;hosts文件&#xff0c;系统参数 2. 添加用户和组信息,…

kafka-sasl消费示范例子

kafka-sasl消费示范例子 package cn.cuiot.dmp.rocketmq;import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec;import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.common.config.SaslConfigs; import org.apache.kafka.common…

20119-1-13作业

一计算类&#xff1a; package count; /** 模拟两个数&#xff0c;加&#xff0c;减&#xff0c;成&#xff0c;除*/ public class Count {//定义第一个数double num1;//定义第二个数double num2;//定义和double sum;//定义做算法的选择int imports;/*** 计算方法*/public …

利用微软Text-To-Speech朗读文本

首先&#xff0c;要安装Microsoft Speech SDK&#xff0c;可以从微软网站上下载&#xff0c;我安装的是SAPI 5.1。安装完毕以后&#xff0c;会附带chm格式的开发文档。 创建一个C# Winform程序&#xff0c;拖一个RichTextBox控件&#xff0c;添加两个启停按钮。界面可以自己做…

了解Browserify

Browserify是一个Javascript的库&#xff0c;可以用来把多个Module打包到一个文件中&#xff0c;并且能很好地应对Modules之间的依赖关系。而Module是封装了属性和功能的单元&#xff0c;是一个Javascript对象&#xff0c;Modules之间可以相互依赖。某种程度上来说&#xff0c;…

数据库的4种隔离级别

数据库事务的隔离级别有4种&#xff0c;由低到高分别为Read uncommitted 、Read committed 、Repeatable read 、Serializable 。而且&#xff0c;在事务的并发操作中可能会出现脏读&#xff0c;不可重复读&#xff0c;幻读。 乐观锁一般会使用版本号机制或CAS算法实现 CAS缺…