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 4584900
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:25:09+00:00 2026-05-21T21:25:09+00:00

Currently I have: 1 file with 9 million lines BufferedReader.readLine() to read every line

  • 0

Currently I have:

  • 1 file with 9 million lines
  • BufferedReader.readLine() to read every line
  • String.split() to parse every line (columns separated by a pipe)
  • A lot of RAM used (because of String interning?)

The problem is: As you may have guessed, I want to read and parse this file a little better…

Questions:

  • How do I read this relatively big file using the least amount of resources (knowing that every line will need some kind of “split” on pipe)?
  • Can I replace String.split by something else (on lets say, StringBuilder, CharBuffer, …)?
  • What’s the best way to avoid using Strings reading the file until I have split them to their final character sequence?
  • I don’t mind using something else then String in my POJOs, if you have anything better?
  • The file will be reload every few hours, if that helps you in giving me a solution?

Thank you 🙂

  • 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-05-21T21:25:10+00:00Added an answer on May 21, 2026 at 9:25 pm

    A 9 million line file should take less than a few seconds. Most of that time will be spent reading the data into memory. How you split up the data is unlikely to make much difference important.

    The BufferedReader and String.split sounds fine to me. I wouldn’t use interning unless you are sure this will help. (It won’t intern() it for you)

    The latest version of Java 6 has some performance improvements in the handling of Strings. I would try Java 6 update 25 to see if it is any faster.


    EDIT: Doing some test finds that split is surprisingly slow and you cna improve on it.

    public static void main(String... args) throws IOException {
        long start1 = System.nanoTime();
        PrintWriter pw = new PrintWriter("deleteme.txt");
        StringBuilder sb = new StringBuilder();
        for (int j = 1000; j < 1040; j++)
            sb.append(j).append(' ');
        String outLine = sb.toString();
        for (int i = 0; i < 1000 * 1000; i++)
            pw.println(outLine);
        pw.close();
        long time1 = System.nanoTime() - start1;
        System.out.printf("Took %f seconds to write%n", time1 / 1e9);
    
        {
            long start = System.nanoTime();
            FileReader fr = new FileReader("deleteme.txt");
            char[] buffer = new char[1024 * 1024];
            while (fr.read(buffer) > 0) ;
            fr.close();
            long time = System.nanoTime() - start;
            System.out.printf("Took %f seconds to read text as fast as possible%n", time / 1e9);
        }
        {
            long start = System.nanoTime();
            BufferedReader br = new BufferedReader(new FileReader("deleteme.txt"));
            String line;
            while ((line = br.readLine()) != null) {
                String[] words = line.split(" ");
            }
            br.close();
            long time = System.nanoTime() - start;
            System.out.printf("Took %f seconds to read lines and split%n", time / 1e9);
        }
        {
            long start = System.nanoTime();
            BufferedReader br = new BufferedReader(new FileReader("deleteme.txt"));
            String line;
            Pattern splitSpace = Pattern.compile(" ");
            while ((line = br.readLine()) != null) {
                String[] words = splitSpace.split(line, 0);
            }
            br.close();
            long time = System.nanoTime() - start;
            System.out.printf("Took %f seconds to read lines and split (precompiled)%n", time / 1e9);
        }
        {
            long start = System.nanoTime();
            BufferedReader br = new BufferedReader(new FileReader("deleteme.txt"));
            String line;
            List<String> words = new ArrayList<String>();
            while ((line = br.readLine()) != null) {
                words.clear();
                int pos = 0, end;
                while ((end = line.indexOf(' ', pos)) >= 0) {
                    words.add(line.substring(pos, end));
                    pos = end + 1;
                }
                // words.
                //System.out.println(words);
            }
            br.close();
            long time = System.nanoTime() - start;
            System.out.printf("Took %f seconds to read lines and break using indexOf%n", time / 1e9);
        }
    }
    

    prints

    Took 1.757984 seconds to write
    Took 1.158652 seconds to read text as fast as possible
    Took 6.671587 seconds to read lines and split
    Took 4.210100 seconds to read lines and split (precompiled)
    Took 1.642296 seconds to read lines and break using indexOf
    

    So it appears that splitting the string yourself is an improvement and gets you close to treading text as fast as you can. The only way to read it faster is to treat the file as binary/ASCII-7. 😉

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

Sidebar

Related Questions

I have a 384MB text file with 50 million lines. Each line contains 2
I have a .csv file containing over 70 million lines of which each line
Currently I just have a file like this that I manually parse into a
I currently have 2 BufferedReader s initialized on the same text file. When I'm
I have a file with a little over a million lines. {<uri::rdfserver#null> <uri::d41d8cd98f00b204e9800998ecf8427e> <uri::TickerDailyPriceVolume>
I currently have a file abc.htm in my Custom Server Control Project and it's
I currently have a class file with the following enumeration: using System; namespace Helper
I currently have a csv file that I'm parsing with an example from here:
I currently have an encoded footer file for a wordpress file I want to
We currently have a process where we receive an inventory file from a vendor

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.