Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8789131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:19:01+00:00 2026-06-13T22:19:01+00:00

I have a log package.I want to filter the log.e, and save it to

  • 0

I have a log package.I want to filter the log.e, and save it to another file. But I find BufferedWriter can not reach the expected effect. Such as the two lines in log file below can not store another file.

E/Vold ( 96): Sleep 2s to make sure that coldboot() events are handled
E/WindowManager( 244): setEventDispatching false

attach code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogSpider {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        BufferedReader bufferedReader = null;

        try {
            bufferedReader = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\log.txt"));
            String line = "";

            try {
                while((line = bufferedReader.readLine())!=null)
                {
                    Parseelog(line);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            try {
                bufferedReader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void Parseelog(String line)
    {
        BufferedWriter bufferedWriter = null;        
        try {
            bufferedWriter = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\logspider2.txt"));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        //Pattern pattern = Pattern.compile("[\\w[.-]]+\\@[\\w[.-]]{2,}\\.[\\w[.-]]+");
        Pattern pattern = Pattern.compile("^E.*");
        Matcher matcher = pattern.matcher(line);

        while(matcher.find())
        {
            String string = new String(matcher.group());
            string += "\n";

            System.out.println(string); //here can print the search results 

            try {
                bufferedWriter.write(string, 0, string.length());
                bufferedWriter.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    

            finally{
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }    
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:19:02+00:00Added an answer on June 13, 2026 at 10:19 pm

    The problem with you code was that you where opening the logspider2.txt file every time you want to write the matching line. So it was overwriting all the previous data. To solve the issue you need to open your file in the append mode as follows:

    bufferedWriter = new BufferedWriter(
                        new FileWriter(
                                "F:\\praful\\androidworkspace_2\\Test\\src\\logspider2.txt",true));
    

    I tried you code and made some modification to make it work. Following is the working code:

        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            BufferedReader bufferedReader = null;
    
            try {
                bufferedReader = new BufferedReader(new FileReader(
                        "F:\\praful\\androidworkspace_2\\Test\\src\\logs.txt"));
                String line = "";
    
                try {
                    while ((line = bufferedReader.readLine()) != null) {
                        Parseelog(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        public static void Parseelog(String line) {
            BufferedWriter bufferedWriter = null;
            // Pattern pattern =
            // Pattern.compile("[\\w[.-]]+\\@[\\w[.-]]{2,}\\.[\\w[.-]]+");
            Pattern pattern = Pattern.compile("^E.*");
            Matcher matcher = pattern.matcher(line);
    
            try {
                bufferedWriter = new BufferedWriter(
                        new FileWriter(
                                "F:\\praful\\androidworkspace_2\\Test\\src\\logspider2.txt",true));
    
                while (matcher.find()) {
                    String string = new String(matcher.group());
                    string += "\n";
    
                    System.out.println(string); // here can print the search results
    
                    bufferedWriter.write(string);
    
                }
    
                bufferedWriter.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
    }
    

    Please changed the file path to your local paths. Hope it help you..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's have an example like below: package xliiv.sandbox; import android.app.Activity; import android.os.Bundle; import android.util.Log;
I have a log method which saves to a file that is named the
I have a log file that I'm trying to append data to the end
I have a log file that I am reading to a string public static
I have a log out button on my main menu and I want it
I have a log file in SqlServer that stores the time an application started,
I have a PHP function that calls a PL/SQL package that can throw a
I have a working FTP system with android, but I want to be able
I have read most posts re this issue, but still can't get my implementation
I'm trying to log all exceptions in an Oracle package. Here's what I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.