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

  • Home
  • SEARCH
  • 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 8553999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:46:55+00:00 2026-06-11T14:46:55+00:00

I am reading a log file through buffered reader mechanism which is taking Total

  • 0

I am reading a log file through buffered reader mechanism which is taking Total execution time taken in millis: 12944 ,please advise how can I improve the performance and bring down this time , Please advise is nio is more better performance than buffered Reader..!! The file size is of 10MB since it is a log file..!! please advise also how this same thing could be achieved with nio also..!!

public class BufferedRedeem
{

    public static void main(String[] args)
    {

        BufferedReader br = null;
        long startTime = System.currentTimeMillis();

        try
        {
            String sCurrentLine;
            br = new BufferedReader(new FileReader("C://abc.log"));

            while ((sCurrentLine = br.readLine()) != null)
            {

            }
            long elapsedTime = System.currentTimeMillis() - startTime;

            System.out.println("Total execution time taken in millis: " + elapsedTime);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (br != null)
                    br.close();
            }
            catch (IOException ex)
            {
                ex.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-11T14:46:56+00:00Added an answer on June 11, 2026 at 2:46 pm

    Since the OP is keen to see how this could be done using NIO.

    As the file is small, it is hard to see the difference but it can be measured.

    public static void main(String... args) throws IOException {
        PrintWriter pw = new PrintWriter("abc.log");
        for (int i = 0; i < 100 * 1000; i++) {
            pw.println("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789");
        }
        pw.close();
    
        long start2 = System.nanoTime();
        int count2 = 0;
        BufferedReader br = new BufferedReader(new FileReader("abc.log"));
        while (br.readLine() != null) count2++;
        br.close();
        long time2 = System.nanoTime() - start2;
        System.out.printf("IO: Took %,d ms to read %,d lines%n", time2 / 1000 / 1000, count2);
    
        long start = System.nanoTime();
        FileChannel fc = new FileInputStream("abc.log").getChannel();
        ByteBuffer bb = ByteBuffer.allocateDirect((int) fc.size());
        fc.read(bb);
        fc.close();
        bb.flip();
    
        CharBuffer cb = ByteBuffer.allocateDirect(bb.remaining() * 2).order(ByteOrder.nativeOrder()).asCharBuffer();
        CharsetDecoder cd = Charset.forName("UTF-8").newDecoder();
        cd.decode(bb, cb, true);
        cb.flip();
        StringBuilder sb = new StringBuilder();
        int count = 0;
        while (cb.remaining() > 0) {
            char ch = cb.get();
            if (isEndOfLine(cb, ch)) {
                // process sb
                count++;
                sb.setLength(0);
            } else {
                sb.append(ch);
            }
        }
        long time = System.nanoTime() - start;
        System.out.printf("NIO as UTF-8: Took %,d ms to read %,d lines%n", time / 1000 / 1000, count);
    
        long start3 = System.nanoTime();
        FileChannel fc2 = new FileInputStream("abc.log").getChannel();
        MappedByteBuffer bb2 = fc2.map(FileChannel.MapMode.READ_ONLY, 0, fc2.size());
        bb.flip();
        StringBuilder sb3 = new StringBuilder();
        int count3 = 0;
        while (bb2.remaining() > 0) {
            char ch = (char) bb2.get();
            if (isEndOfLine(bb2, ch)) {
                // process sb
                count3++;
                sb3.setLength(0);
            } else {
                sb3.append(ch);
            }
        }
        fc2.close();
        long time3 = System.nanoTime() - start3;
        System.out.printf("NIO as ISO-8859-1: Took %,d ms to read %,d lines%n", time3 / 1000 / 1000, count3);
    
    
    }
    
    private static boolean isEndOfLine(CharBuffer cb, char ch) {
        if (ch == '\r') {
            if (cb.remaining() >= 1 && cb.get() == '\n') {
                return true;
            }
            cb.position(cb.position() - 1);
            return true;
        } else if (ch == '\n') {
            return true;
        }
        return false;
    }
    
    private static boolean isEndOfLine(ByteBuffer bb, char ch) {
        if (ch == '\r') {
            if (bb.remaining() >= 1 && bb.get() == '\n') {
                return true;
            }
            bb.position(bb.position() - 1);
            return true;
        } else if (ch == '\n') {
            return true;
        }
        return false;
    }
    

    prints each line is 102 bytes long so the file is ~ 10 MB.

    IO: Took 112 ms to read 100,000 lines
    NIO as UTF-8: Took 207 ms to read 100,000 lines
    NIO as ISO-8859-1: Took 87 ms to read 100,000 lines
    

    As I mentioned before, its unlikely to be worth the extra complexity of using NIO to save 35 ms.

    BTW: If you have a HDD and the file is not in memory, only the speed of your drive will matter.

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

Sidebar

Related Questions

I have a log file that I am reading to a string public static
I'm reading in data about an HTTP access log. I've got a file with
I have a service application which I will be soon implementing a log file.
I am reading a postfix mail log file into an array and then looping
What is the most efficient(fast and safe) way of reading a log file in
I am reading a binary log file produced by a piece of equipment. I
After reading this article http://lukast.mediablog.sk/log/?p=155 I decided to use mingw on linux to compile
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
Can anyone give me the working example of reading two files simultaneously through threads?
I'm using $.get to run through an XML file & return a the values

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.