Java 缓存管道流(BufferedReader)按行读取文件

输入文件

/home/jerry/304.log

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.geo.cn</groupId>
    <artifactId>DASink</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <dependencies>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <!-- 此处指定main方法入口的class -->
                            <mainClass>com.geo.cn.pojo.Test</mainClass>
                        </manifest>
                    </archive>
                  </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

        </plugins>
    </build>




</project>

代码

package com.geo.cn.pojo;

import java.io.*;
public class Test {
   

    public static void main(String[] args) {
        BufferedReader bf_read = null;
        BufferedWriter bf_write = null;
        try {
            //BufferedReader bf = new BufferedReader(new FileReader(args[0]));
            bf_read = new BufferedReader(new InputStreamReader(new FileInputStream("/home/jerry/304.log")));
            bf_write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/home/jerry/indx304.log")));
            String str;

            try {
                while ((str = bf_read.readLine()) !=null){
                    //System.out.println("数据长度"+str.split("\t").length);
                    //System.out.println(str.split("\t"));

                    if(!"".equals(str) && null != str.split("\t",-1)[11]  && !"".equals(str.split("\t",-1)[11] )){
                        str = str.replace("消息: (null, ","");

                        //String [] res = str.split("\t",-1);
                        //System.out.println("===============  "+res[11]);
                        //System.out.println("$$$$$$$$$$$$$$$$$$$$"+str);
                        int indexFlag = str.indexOf(")");

                        str = str.substring(0,indexFlag);



                        System.out.println("----------------"+str);
                        bf_write.write(str);
                        bf_write.newLine();

                    }else{
                        System.out.println("###########  "+str);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();

            }finally {
                try {
                    bf_read.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    bf_write.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

打包执行

  • 打包
mvn package
  • 执行
(文件名):/home/jerry/304.log 
java   -jar Test.jar    /home/jerry/304.log 
发布了150 篇原创文章 · 获赞 15 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/dymkkj/article/details/100878677