您的位置:

java中日志文件的读取,java查看日志的命令

本文目录一览:

java快速读取日志文件最后一行数据,日志文件非常大。

package cn.com.songjy.test.io;  

  

import java.io.FileNotFoundException;  

import java.io.IOException;  

import java.io.RandomAccessFile;  

  

/** 

 * 从最后一行开始读取 

 */  

public class FromEndRF {  

  

    /** 

     *  

     * @param filename 目标文件 

     * @param charset 目标文件的编码格式 

     */  

    public static void read(String filename, String charset) {  

  

        RandomAccessFile rf = null;  

        try {  

            rf = new RandomAccessFile(filename, "r");  

            long len = rf.length();  

            long start = rf.getFilePointer();  

            long nextend = start + len - 1;  

            String line;  

            rf.seek(nextend);  

            int c = -1;  

            while (nextend  start) {  

                c = rf.read();  

                if (c == '\n' || c == '\r') {  

                    line = rf.readLine();  

                    if (line != null) {  

                        System.out.println(new String(line  

                                .getBytes("ISO-8859-1"), charset));  

                    } else {  

                        System.out.println(line);  

                    }  

                    nextend--;  

                }  

                nextend--;  

                rf.seek(nextend);  

                if (nextend == 0) {// 当文件指针退至文件开始处,输出第一行  

                    // System.out.println(rf.readLine());  

                    System.out.println(new String(rf.readLine().getBytes(  

                            "ISO-8859-1"), charset));  

                }  

            }  

        } catch (FileNotFoundException e) {  

            e.printStackTrace();  

        } catch (IOException e) {  

            e.printStackTrace();  

        } finally {  

            try {  

                if (rf != null)  

                    rf.close();  

            } catch (IOException e) {  

                e.printStackTrace();  

            }  

        }  

    }  

  

    public static void main(String args[]) {  

        read("mynewfile.txt", "UTF-8");  

    }  

}

说明:以上是一个倒着读取文件的案例,可以根据此案例,结合自己的实际需求来设计自己的代码。另外,如果是web应用或其它持续型应用,可以尝试利用单独线程保持文件打开或将读取位置存储在内存中。

java怎么读取windows 事件日志 文件

您好,很高兴为您解答。日志文件在哪个目录,用fileinputstream读文件就行了

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class FileReaderTest {

static ListString logs = new ArrayListString();

public static ListString readFile(File f) throws IOException {

if (f == null || !f.exists()) {

return null;

}

ListString list = new ArrayListString();

String encoding = "UTF-8";

InputStreamReader read = new InputStreamReader(new FileInputStream(f),

encoding);

BufferedReader br = new BufferedReader(read);

String line = null;

java定时读取日志文件

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.io.RandomAccessFile;

import java.io.ByteArrayInputStream;

class Reader implements Runnable {

Reader(String filename) {

this.filename = filename;

}

private String filename;

private long filelength = 0;

private int count=0;

@Override

public void run() {

while (true) {

try {

File f = new File(filename);

long nowlength = f.length();

long readlength = nowlength - filelength;

if (readlength == 0) {

Thread.sleep(1000);

continue;

}

RandomAccessFile rf = new RandomAccessFile(f, "r");

// 移动文件指针到上次读的最后

rf.seek(filelength);

filelength=nowlength;

byte[] b = new byte[(int) readlength];

rf.read(b, 0, b.length);

rf.close();

BufferedReader br=new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));

String str=null;

count++;

System.out.println("第"+count+"次读到的内容:");

while((str=br.readLine())!=null){

System.out.println(str);

}

br.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

class Writer implements Runnable{

Writer(String filename) {

this.filename = filename;

}

private String filename;

private int count=0;

@Override

public void run() {

while (count++100){

try {

PrintWriter pw=new PrintWriter(new FileWriter(filename,true));

pw.append(""+count).append("\t").append(""+System.currentTimeMillis()).append("写入的内容").append("\r\n");

pw.close();

Thread.sleep(100);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

public class ReadFileFromTrail {

public static void main(String[] args) {

Reader reader=new Reader("d:\\test.log");

Writer writer=new Writer("d:\\test.log");

new Thread(reader).start();

new Thread(writer).start();

}

}

log4j的使用,即java该如何使用日志文件

java使用日志文件log4j的方法:

1、 新建一个Java工程,导入Log4j包,pom文件中对应的配置代码如下:

!-- log4j support --

dependency

   groupIdlog4j/groupId

   artifactIdlog4j/artifactId

   version1.2.17/version

/dependency

2、resources目录下创建log4j.properties文件

### 设置###

log4j.rootLogger = debug,stdout,D,E

### 输出信息到控制抬 ###

log4j.appender.stdout = org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target = System.out

log4j.appender.stdout.layout = org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 输出DEBUG 级别以上的日志到=/home/duqi/logs/debug.log ###

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender

log4j.appender.D.File = /home/duqi/logs/debug.log

log4j.appender.D.Append = true

log4j.appender.D.Threshold = DEBUG

log4j.appender.D.layout = org.apache.log4j.PatternLayout

log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出ERROR 级别以上的日志到=/home/admin/logs/error.log ###

log4j.appender.E = org.apache.log4j.DailyRollingFileAppender

log4j.appender.E.File =/home/admin/logs/error.log

log4j.appender.E.Append = true

log4j.appender.E.Threshold = ERROR

log4j.appender.E.layout = org.apache.log4j.PatternLayout

log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

3、输出日志的例子如下

package com.javadu.log;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class Log4JTest {

   private static final Logger logger = LoggerFactory.getLogger(Log4JTest.class);

   public static void main(String[] args) {

       // 记录debug级别的信息

       logger.debug("This is debug message.");

       // 记录info级别的信息

       logger.info("This is info message.");

       // 记录error级别的信息

       logger.error("This is error message.");

   }

}

4、输出结果

首先,控制台输入如下图所示:

然后,查看/Users/duqi/logs目录下的debug.log和error.log文件,内容分别如下,可以看出: