how to remove particular lines when reading text file in java - java

i want to delete that line where it finds "RxTracker"
this is my text file
INFO http-bio-80-exec-1 root - Received data;863071018134228;12.964624;77.523682;NE;23.22376;3.82;0;2013-01-06^08:41:00;
INFO http-bio-80-exec-1 root - RxTracker; IMEINO is 863071018134228
INFO http-bio-80-exec-2 root - RxTracker Invoked. Reading Parameters
INFO http-bio-80-exec-2 root - Received data;863071018134228;12.964624;77.523682;NE;37.66936;3.82;0;2013-01-06^08:42:52;
INFO http-bio-80-exec-2 root - RxTracker; IMEINO is 863071018134228
INFO http-bio-80-exec-5 root - RxTracker Invoked. Reading Parameters
INFO http-bio-80-exec-5 root - Received data;863071018134228;12.964624;77.523682;NE;20.92728;3.82;0;2013-01-06^08:44:51;
INFO http-bio-80-exec-5 root - RxTracker; IMEINO is 863071018134228
INFO http-bio-80-exec-3 root - RxTracker Invoked. Reading Parameters
this is my java code
public void Insert1()
{
try{
File f=new File("E:/c.txt");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
int lineNum = 0;
String line = null;
while ( (line = br.readLine() ) != null ) {
lineNum++;
if(br.readLine().equalsIgnoreCase("RxTracker"))
{
System.out.println("ji");
}
else
{
System.out.println("ji");
}
//if ( lineNum %2 == 0 ) continue;
//else deal with it
System.out.println(br.readLine());
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e1){
e1.printStackTrace();
}

Try something like this. The code reads each line of a file and writes it to another file.Every time it checks if that line contains RxTracker If that line doesn't contain the RxTracker then it will write that line to the new file and if it contains that string then it will skip that line,
for eg
String lineToRemove = "RxTracker ;
if(!trimmedLine.contains(lineToRemove)) { // check if t he line does not contains it then write it to another file
writer.write(trimmedLine);
writer.newLine();
}

Related

How do I use Java to incrementally read line from file?

I want to use Java to read line by line from input file. The logic of the code should be:
The loadFile() in the log class reads the first line. The deltaRecords stores the first line
In the Main class, I call loadFile(), and it only upload the data in the deltaRecords, which is the 1st line. The loadFile() waits until the first line has been analyzed by testRecords() in Main class
The loadFile() in the log class reads the 2nd line. The deltaRecords stores the 2nd line
In the Main class, I call loadFile(), and it only upload the data in the deltaRecords, which is the 2nd line. The loadFile() waits until the 2nd line has been analyzed by testRecords() in Main class
The loadFile() in the log class reads the 3rd line. The deltaRecords stores the 3rd line.
For example, my input file is:
TOM 1 2 <br/>
Jason 2 3 <br/>
Tommy 1 4 <br/>
After I read TOM 1 2. I need to halt the process and do the analysis. Then I continue to read Jason 2 3.
Here is my current code. In the log class, I defined loadFile() to read the input line by line. In the Main class, I defined testRecords() to analyze.:
public void loadFile(String filename) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
Main main = new Main();
String line = br.readLine();
int count = 0;
while (line != null) {
if (line.length() == 0) {
line = br.readLine();
continue;
}
LogRecord record = new LogRecord(line);
//add record to the deltaRecord
deltaRecords.add(record);
//here I need to wait until it has been analyzed. Then I continue read
//initial the deletaRecords and add current line to the old record
existRecords.add(record);
line = br.readLine();
}
}catch(Exception e){
System.out.println("load log file failed! ");
e.printStackTrace();
}
In my main function, I call loadfile:
Log log = new Log();
log.loadFile("data/output_01.txt");
QueryEngine.queryEngine.log = log;
testRecord(log);
Could anyone tell me how to solve this problem? Thank you very much.
You don't need to read a line when you create your line variable
Replace
String line = br.readLine();
with
String line = null;
You can use value assignment inside a conditional
Replace
while (line != null) {
with
while ((line = br.readLine()) != null) {
In the case of empty lines continue
Replace
if (line.length() == 0) {
line = br.readLine();
continue;
}
with
if (line.length() == 0) {
continue;
}
How your code should look like
String line = null;
while ((line = br.readLine()) != null) {
if (line.length() == 0) continue;
//Analyze your line
}
You can solve it using a Consumer lambda function as input on your loadFile as follow:
public void loadFile(String filename, Consumer<LogRecord> lineAnalyzer) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
Main main = new Main();
String line = br.readLine();
int count = 0;
while (line != null) {
if (line.length() == 0) {
line = br.readLine();
continue;
}
LogRecord record = new LogRecord(line);
//add record to the deltaRecord
deltaRecords.add(record);
//here I need to wait until it has been analyzed. Then I continue read
//Analyze line
lineAnalyzer.accept(record);
existRecords.add(record);
line = br.readLine();
}
}catch(Exception e){
System.out.println("load log file failed! ");
e.printStackTrace();
}
Then when you call the loadFile function you can specify the analysis line logic as follow:
Log log = new Log();
QueryEngine.queryEngine.log = log;
log.loadFile("data/output_01.txt",logRecord -> {
System.out.println("Starting analysis on log record "+logRecord);
// Analyze the log record
testRecord(log);
});
You can use scanner to read the file and process line by line. I have attached snippet code below.
public void loadFile(String filename) {
try {
Scanner sc= new Scanner(new File(filename));
while (sc.hasNext()) {
String line = sc.nextLine();
LogRecord record = new LogRecord(line);
//add record to the deltaRecord
deltaRecords.add(record);
//here I need to wait until it has been analyzed. Then I continue read next line
//Place your code here
}
} catch (FileNotFoundException fe) {
System.err.println(fe);
}
}

Find String in text file, delete line and ones below

I have a bit of code to find a string in a text file, print the line the string is on and then print the 5 lines below it. However, I need to modify it so that instead of printing, it deletes/removes the line after the string is found. How would I go about doing this?
File file = new File("./output.txt");
Scanner in = null;
try {
in = new Scanner(file);
while (in.hasNext()) {
String line = in.nextLine();
if (line.contains("(1)")) {
for (int a = 0; in.hasNextLine() && a < 6; a++) {
System.out.println(line);
line = in.nextLine();
}
}
}
} catch (Exception e) {
}
Find a small snippet you can start with.
Assuming your question.txt has the following input.
line 1
line 2
line 3 (1)
line 4
line 5
line 6
line 7
line 8
line 9
line 10
This snippet will print all lines and skip the line line 3 (1) as well the five lines after.
List<String> lines = Files.readAllLines(Paths.get("question.txt"), Charset.defaultCharset());
for (int i = 0; i < lines.size(); i++) {
if (lines.get(i).contains("(1)")) {
i = i + 6;
}
System.out.println(lines.get(i));
}
output
line 1
line 2
line 9
line 10
To store the lines into the file is left for you.
My Suggestion is you first declare and initialise a StringBuilder say output before your above code like:
StringBuilder output = new StringBuilder();
Now after the close of the if statement before the closing of the while loop append the line to the output and add a "\n" at the end like this:
output.append(line+"\n");
Now finally after your code that you have posted create a FileWriter say writer and then use the writer to write the output as shown below:
try(FileWriter writer = new FileWriter(file, false)){
writer.write(output);
}catch IOException(e){
e.printStackTrace();
}
Also don't forget to remove or comment out the following line if you do not want them printed in the output.
System.out.println(line);
SubOtimal has a good, concise answer that will work for most cases. The following is more complex but avoids loading the whole file into memory. That probably isn't an issue for you but just in case...
public void deleteAfter(File file, String searchString, int lineCountToDelete) {
// Create a temporary file to write to
File temp = new File(file.getAbsolutePath() + ".tmp");
try (BufferedReader reader = new BufferedReader(new FileReader(file));
PrintWriter writer = new PrintWriter(new FileWriter(temp)) ) {
// Read up to the line we are searching for
// and write each to the temp file
String line;
while((line = reader.readLine()) != null && !line.equals(searchString)){
writer.println(line);
}
// Skip over the number of lines we want to "delete"
// as well as watching out for hitting the end of the file
for(int i=0;i < lineCountToDelete && line != null; i++){
line = reader.readLine();
}
// Write the remaining lines to the temp file.
while((line = reader.readLine()) != null){
writer.println(line);
}
} catch (IOException e) {
throw new IllegalStateException("Unable to delete the lines",e);
}
// Delete the original file
if(!file.delete()){
throw new IllegalStateException("Unable to delete file: " + file.getAbsolutePath());
}
// Rename the temp file to the original name
if(!temp.renameTo(file)){
throw new IllegalStateException("Unable to rename " +
temp.getAbsolutePath() + " to " + file.getAbsolutePath());
}
}
I tested this with multiple conditions, including a line that doesn't exist, a line at the end and a line with fewer lines left than the number to skip. All worked and gave the appropriate results.

Line when reading a file is empty but the line is not null

I have a problem in java and i dont understand why, since i think i am doing text-book stuff.
An overview in what of want to do is:
I want to create a file that contains in each line two strings: documentPath, documentID (in this format: "documentPath;documentID;")
I want to be able to add lines at the end of the file and load the file to a Java Data Structure, lets say a HashSet.
Each time i want to add a new line, i load all the file in a HashSet, check if the line i want to add is not already there and eventually add it at the end. (small number of data - don't care about efficiency)
The code
Add file:
public void addFile(String documentPath) {
this.loadCollection(); //METHOD IS NOT CONTINUING: ERROR HERE
if (!documentsInfo.contains(documentPath)) {
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(this.collectionFile, true)));
DocumentInfo documentInfo = new DocumentInfo(documentPath, ++this.IDcounter);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Load file:
public void loadCollection() {
if (loaded) {return;}
BufferedReader br;
try {
br = new BufferedReader(new FileReader(collectionFile));
String line;
while ( (line = br.readLine())!= null ) { //PROBLEM HERE
System.out.println("the line readed from file-" + line + "-");
System.out.println("is the line null: "+ (line==null));
System.out.println("line length: " + line.length());
DocumentInfo documentInfo = new DocumentInfo(line);
documentsInfo.add(documentInfo);
}
br.close();
open = true;
} catch (IOException e) {
e.printStackTrace();
}
}
create the line to add:
public DocumentInfo(String fileLine) {
String delimiter = Repository.DOCUMENT_FILE_SEPARATOR;
StringTokenizer tok = new StringTokenizer(fileLine, delimiter);
System.out.println("Tokenizer starts with string: " + fileLine);
this.documentPath = tok.nextToken(); //EXCEPTION here
this.documentId = Integer.parseInt(tok.nextToken());
}
public String toString() {
String sep = Repository.DOCUMENT_FILE_SEPARATOR;
return this.getDocumentPath()+sep+this.getDocumentId()+sep+"\n";
}
I am getting the exception at the Tokenizer method (java.util.NoSuchElementException) when i try to get the nextToken, but the problem comes from the loadCollection() method. The first time i read the contents of the file nothing is there, the line is empty (lenght: 0) but the line is not null, so the while-condition fails to stop the while iteration.
Here is what i get from the debbuging prints:
the line readed from file--
is the line null: false
line length: 0
Tokenizer starts with string:
Can anyone help me with this?
You get a null only when you have exhausted the stream. But the first line of the stream (your file) is just an empty line - and you load it, the result of the empty line, is an empty string (""). It can be easily solved by skipping lines with string.length() == 0, by adding the following in your while loop:
if (line.length() == 0) continue;
You might want to consider using trim() before checking the length as well, to avoid nasty spaces making the string.length() > 0

want to start reading a file from a specific point

I have a file that contains log print out and I want to read certain lines and from certain point from the middle to the end of the line and move to the next line that match the criteria.
I want to start reading from RULE EXECUTING to the end of the line and then check the next line of it has RULE EXECUTING if not skip it to the following line if that does have RULE EXECUTING then copy from that point to the end of the line.
FILE SAMPLE
2013-02-14 09:26:20:078 [main] DEBUG sne.ABC.hdhdh.jfjjfj.jkfjfjd.jdsd - RULE EXECUTING --> CMNETSL.hdjjjdlskdnlskd.jgfkdflkdfl_Translation
2013-02-14 09:28:00:312 [main] DEBUG moc.uty.lweifoisd.sfsd.kjfdnkjs.RulesetInvoker - Rudejgfjkgjf: After invoking: CMNETSLO
2013-02-14 09:26:20:421 [main] DEBUG sne.ABC.hdhdh.jfjjfj.jkfjfjd.jdsd - RULE EXECUTING --> sne.ABC.hdhdh.jfjjfj.jkfjfjd.jdsd
what I want to get from the line would look like this
RULE EXECUTING --> CMNETSL.hdjjjdlskdnlskd.jgfkdflkdfl_Translation
RULE EXECUTING --> sne.ABC.hdhdh.jfjjfj.jkfjfjd.jdsd
So, something like:
BufferedReader br = new BufferedReader(new FileReader("mylog.log"));
String line;
int idx;
while((line = br.getLine()) != null)
if((idx = line.indexOf("RULE EXECUTING --> ")) != -1)
System.out.println(line.substring(idx));
I've not tried that in an ide or compiled it, but I think that you probably get the idea.
Scanner s = new Scanner(new File("log.txt")); // read file using scanner line by line
while(s.hasNextLine())
{
String nextLine = s.nextLine();
if(nextLine.startsWith("RULE EXECUTING")) //check if line starts with the key word
{
// do whatever you want to do
}
}
You have to read the entire file for that, use a FileReader and read the file line for line, searching for the substring "RULE EXECUTING" in each. If it is found, use the substring method to output or otherwise process the substring. Repeat until the file is done and - most importantly - close the file.
Here is a complete codesample:
BufferedReader reader = null;
try {
//open file
reader = new BufferedReader(new FileReader("example.log"));
int index;
//read first line
String line = reader.readLine();
//go through the entire file line for line
while (line != null) {
//look for substring in line
index = line.indexOf("RULE EXECUTING");
if(index >= 0 ) {
//substring found, write result to stdout
System.out.println(line.substring(index));
}
//read next line
line = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//always close file readers!
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}

reading log file in java

Is it possible to read log files (abc.log) using java?
I want a specific string from my log file.
suppose this is the content of my logfile. I want the time stamp only (eg: 05:08:37) and print it the console.
2012-12-16 05:08:37,905 [Thread-1] INFO com.submit.SubmitService - Wait time 500
2012-12-16 05:08:38,444 [Thread-1] INFO com.submit.SubmitService - NO OF RECORDS TILL NOW 3755 TOTAL TIME -- << 539
2012-12-16 05:08:38,668 [Thread-1] INFO com.submit.SubmitService - Active Connection:: -69076
2012-12-16 05:08:38,670 [Thread-1] INFO com.submit.SubmitService - Active Connection:: -65764
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();
}

Categories

Resources