reading log file in java

流过昼夜 提交于 2019-12-08 13:46:32
Luca Davanzo

You can read your "log-file" as a normal file.

Then you can use, for instance, regular expression, to obtain the part of the string that you need:

try{
   FileInputStream fstream = new FileInputStream("abc.log");
   BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
   String strLine;
   /* read log line by line */
   while ((strLine = br.readLine()) != null)   {
     /* parse strLine to obtain what you want */
     System.out.println (strLine);
   }
   fstream.close();
} catch (Exception e) {
     System.err.println("Error: " + e.getMessage());
}

import java.io.File;import java.io.FileNotFoundException;importjava.util.Scanner; import java.sql.*;

public class ReadingEntireFileWithoutLoop {

public static void main(String[] args) throws FileNotFoundException {

    File file = new File("X:\\access.log");

    Scanner sc = new Scanner(file);
    sc.useDelimiter(",|\r\n");
    System.out.println(sc.next());
    while(sc.hasNext()){
        System.out.println(sc.next());
        }
        // closing the scanner stream
        sc.close();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!