Java Log Helper using Scanner
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikmTpxmVye7av58vpJJyvAicxST15rhg91riuWEmp_-SY9FFDJj5SVPvpBAYQe-8wEvK4Ee3zV6GOP1l7_ydlphHHXo8u_WP-gBaDGlq15edZu_QAoxF6hvSht0UXG8w_ESr61UiFgiuDX/s400/Screenshot-2015-10-12_13.01.10.jpg)
Through this utility, without opening the log file and effortlessly we can find the string we are looking for within the log file. If there is a match the utility will return a match along with the line numbers the string you are looking for is present.
From the sample.txt file I am finding the string "com0113:
package sudas.utils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class LogScannerHelper {
public static void main(String[] args) {
// new Bufferedreader().inputStream();
new scanner().scanLines();
}
}
class scanner {
// read the content of the log file and search for a perticular string
public void scanLines() {
try {
Scanner scan = new Scanner(new File("C:/Test/sudas.txt"));
String match = "of";
int lineNumber = 1;
while (scan.hasNextLine()) {
String line = scan.nextLine();
// System.out.println(line+'\t'+lineNumber++);
Scanner string = new Scanner(line);
String completeString = string.findInLine("companies");
{
int lineNum = lineNumber++;
if (completeString != null) {
System.out.println(completeString + '\t' + "-- at line number "+lineNum);
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
No comments:
Post a Comment