Java Program to Read Specific contain from Files


Introduction


This program helps us to verify and retrieve some information based on specific text available in the file. it will verify text against the file and will retrieve next few lines of information from the same type of files available at the given folder location.
For Example, We have 100 logs files under Logs folder and the user wants to search and retrieve all Record Not Found exception with next 5 lines of information then User can use this program and write the information on the console.
It will help User to reduce their time and effort. Also, it will be more efficient in term of comparing the same set of information within a file which occurred across 100 log files.
Please follow below steps to understand and execute the program on your local system.


Java Program


Please find java program to find frequency of the digit below –

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;

/**
* This class helps to read data/records from set of files.
*
* @author dsahu1
*
*/
public class ReadContentsFromFiles {

/**
* This method helps to execute the program.
*
* @param args
*/
public static void main(String[] args) {

// Call method to start business logic to retrieve information
readContentsFromFilesByText(
"C:/Logs/ABC/20171104/Streams",
"Exception", ".txt",3);

}

/**
* This method helps to read data from Files based on passed text.
*
* @param filesPath
* @param textToSearch
* @param fileType
*/
private static void readContentsFromFilesByText(String filesPath,
String textToSearch, final String fileType, int numberOfLinePrint) {

// Instantiate FilenameFilter filter
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(fileType);
}
};

File folder = new File(filesPath);
// Retrieve list of Files available
File[] listOfFiles = folder.listFiles(filter);

// Iterate all Files one by one to Read Text Information
for (int i = 0; i < listOfFiles.length; i++) {
File file = listOfFiles[i];
String line = null;
String fileName = file.getName();
// Initiate count to capture number of line in the output
int count = 0;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(file);

// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
// Boolean variable to hold contents availability
boolean isContaintFound = false;
// Read Files
while ((line = bufferedReader.readLine()) != null) {
// Check passed text to search
if (line.contains(textToSearch)) {
isContaintFound = true;
System.out.println(line);
count++;
continue;
}else if(isContaintFound && count < numberOfLinePrint){
count++;
System.out.println(line);
}else if (isContaintFound && count == numberOfLinePrint) {
System.out.println(line);
isContaintFound = false;
}

}
if (count != 0) {
System.out.println("****File Name **** " + fileName
+ " Total Occurence :" + count);
count = 0;
}
bufferedReader.close();
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
} catch (IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
}
}
}

}


Output(s)


We have to pass below input in order execute this program successfully, Please find them below –

Parameter Name Value Description
filesPath C:/Logs/ABC/20171104/Streams This parameter hols the path where all files are available.
textToSearch Exception This parameter holds the text which User wants to search in the file.
fileType .txt This parameter hold the type of file like text or log etc.
numberOfLinePrint 3 This parameter holds the value which indicates how many lines you want from the files once you text matched.

 

Please find sample output below –

[batchlauncher] The processing for the record with '47081999254' was skipped, with error message '[PRosterLineItemID '64613701135' Could not be Approved. Reason 'curam.util.exception.InformationalException: The estimated cost cannot have a negative value, the amount must be zero or more.
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:367)
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:326)
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:310)
[batchlauncher] The processing for the record with '47081999254' was skipped, with error message '[PRosterLineItemID '64613701135' Could not be Approved. Reason 'curam.util.exception.InformationalException: The estimated cost cannot have a negative value, the amount must be zero or more.
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:367)
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:326)
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:310)
[batchlauncher] The processing for the record with '47081999254' was skipped, with error message '[PRosterLineItemID '64613701135' Could not be Approved. Reason 'curam.util.exception.InformationalException: The estimated cost cannot have a negative value, the amount must be zero or more.
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:367)
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:326)
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:310)
[batchlauncher] The processing for the record with '47081999254' was skipped, with error message '[PRosterLineItemID '64613701135' Could not be Approved. Reason 'curam.util.exception.InformationalException: The estimated cost cannot have a negative value, the amount must be zero or more.
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:367)
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:326)
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:310)
[batchlauncher] The processing for the record with '47081999254' was skipped, with error message '[PRosterLineItemID '64613701135' Could not be Approved. Reason 'curam.util.exception.InformationalException: The estimated cost cannot have a negative value, the amount must be zero or more.
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:367)
[batchlauncher] at curam.util.exception.InformationalManager.buildAllExceptionsNoTrace(InformationalManager.java:326)
[batch

Leave a Reply

Your email address will not be published. Required fields are marked *