簡體   English   中英

Java中的日志文件自動監視每次都會增加,並且總是希望監視新行

[英]Log file auto monitoring in java which is incrimenting every time and always want to monitor new line

我有一個日志文件,該文件一直在增加,這是因為在我的應用程序中記錄了不同的日志記錄事件。 現在,我想檢查日志文件中的某些模式,如果找到與模式匹配的任何內容,則將其打印在其他位置。 日志文件不是靜態文件,它總是在增加,我的程序不應該再次檢查舊行。 它應始終檢查尚未檢查的新行。

將您要檢查的內容的位置存儲在某種整數或文件中。 現在,當您尋找模式時,尋找正則表達式 ,然后將其打印到任何輸出流。

首先像這樣將文件讀入數組,然后調用getWordsFromFile()方法。

/**
 *
 * @param fileName is the path to the file or just the name if it is local
 * @return the number of lines in fileName
 */
public static int getLengthOfFile(String fileName) {
    int length = 0;
    try {
        File textFile = new File(fileName);
        Scanner sc = new Scanner(textFile);
        while (sc.hasNextLine()) {
            sc.nextLine();
            length++;
        }
    } catch (Exception e) {

    }
    return length;
}

/**
 *
 * @param fileName is the path to the file or just the name if it is local
 * @return an array of Strings where each string is one line from the file
 * fileName.
 */
public static String[] getWordsFromFile(String fileName) {
            int lengthOfFile = getLengthOfFile(fileName);
    String[] wordBank=new String[lengthOfFile];
    int i = 0;
    try {
        File textFile = new File(fileName);
        Scanner sc = new Scanner(textFile);
        for (i = 0; i < lengthOfFile; i++) {
        wordBank[i] = sc.nextLine();
        }
        return wordBank;
    } catch (Exception e) {
                System.err.println(e);
        System.exit(55);
    }
    return null;
}

對於較大的文件,請參見以下問題。然后,一旦加載String [],就可以像這樣迭代。

for(int i=0;i<arr.length;i++) 
doSomething(arr[i]);

for(int i= start;i<arr.length;i++)
doSomething(arr[i]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM