Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Reading a textfile into a single String?

Thread view: 
Digital Puer - 22 Feb 2006 20:48 GMT
Hi, I'm trying to read multiple text lines from a file into a single
String. The approach I am using is to read each line of the file
with BufferedReader.readLine() and appending it to a String,
but this is really slow:

       BufferedReader br = new BufferedReader(fr);
       String buffer, result;
       while ((buffer = br.readLine()) != null) {
               result = result + buffer;
       }

Is there a better, faster way?
tom fredriksen - 22 Feb 2006 21:09 GMT
> Hi, I'm trying to read multiple text lines from a file into a single
> String. The approach I am using is to read each line of the file
[quoted text clipped - 6 lines]
>                 result = result + buffer;
>         }

first of all you should be using StringBuffer.append() instead of String
concatenation, its many many times faster.

Secondly, there is nothing stopping you from using the method
    BufferedReader.read(char []...)
with a char array of, say, 1-8 KB, which you add to the StringBuffer
repeatedly. Just make sure you set the size of the BufferedReader to at
least the array size.

/tom
Jeffrey Schwab - 22 Feb 2006 22:33 GMT
>>         BufferedReader br = new BufferedReader(fr);
>>         String buffer, result;
>>         while ((buffer = br.readLine()) != null) {
>>                 result = result + buffer;
>>         }

...

> Secondly, there is nothing stopping you from using the method
>     BufferedReader.read(char []...)
> with a char array of, say, 1-8 KB, which you add to the StringBuffer
> repeatedly. Just make sure you set the size of the BufferedReader to at
> least the array size.

That's a much nicer solution.  It is a little different from the
original though, in that it preserves line terminators.
Digital Puer - 23 Feb 2006 00:02 GMT
Thank you for the clear explanation.

> > Hi, I'm trying to read multiple text lines from a file into a single
> > String. The approach I am using is to read each line of the file
[quoted text clipped - 17 lines]
>
> /tom
Jeffrey Schwab - 22 Feb 2006 22:24 GMT
> Hi, I'm trying to read multiple text lines from a file into a single
> String. The approach I am using is to read each line of the file
[quoted text clipped - 5 lines]
>         while ((buffer = br.readLine()) != null) {
>                 result = result + buffer;

On the first pass, "result" has not been initialized.

>         }
>
> Is there a better, faster way?

Use a StringBuilder or a StringBuffer.  The StringBuilder theoretically
should be a little faster, because it does not provide any guarantee of
synchronization.  On my system, they are equally fast.  In the program
below, each takes ~300ms to process the input file, plus a 50-ms hit to
whichever is tested first.  Here's the output of a run on my system:

    > java -cp . Main
    Using a StringBuilder took 350 ms.
    Using a StringBuffer took 301 ms.
    ^C

OK, so I killed the String test.  It was slowing down my machine.
Here's the code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Date;

class Main {

    static String readWithString(Reader fr)
        throws IOException {

        BufferedReader br = new BufferedReader(fr);
        String buffer, result = "";
        while ((buffer = br.readLine()) != null) {
            result = result + buffer;
        }

        return result;
    }

    static String readWithStringBuffer(Reader fr)
        throws IOException {

        BufferedReader br = new BufferedReader(fr);
        String line;
        StringBuffer result = new StringBuffer();
        while ((line = br.readLine()) != null) {
            result.append(line);
        }

        return result.toString();
    }

    static String readWithStringBuilder(Reader fr)
        throws IOException {

        BufferedReader br = new BufferedReader(fr);
        String line;
        StringBuilder result = new StringBuilder();
        while ((line = br.readLine()) != null) {
            result.append(line);
        }

        return result.toString();
    }

    public static void main(String[] args)
        throws IOException {

        /* Using each of several buffer types, record the time taken to
         * concatenate all lines from a text file, without newlines.
         *
         * The sample text file is ~ 3MB, courtesy of Project Gutenberg.
         * http://www.gutenberg.org/
         */

        FileReader f = new FileReader("war_and_peace.txt"); // ~3MB

        try {

            long t = new Date().getTime();
            readWithStringBuilder(f);
            t = new Date().getTime() - t;
            System.out.println(
                "Using a StringBuilder took " + t + " ms.");
        } finally {
            f.close();
        }

        f = new FileReader("war_and_peace.txt"); // ~3MB.

        try {
            long t = new Date().getTime();
            readWithStringBuffer(f);
            t = new Date().getTime() - t;
            System.out.println(
                "Using a StringBuffer took " + t + " ms.");
        } finally {
            f.close();
        }

        f = new FileReader("war_and_peace.txt"); // ~3MB.

        try {
            long t = new Date().getTime();
            readWithString(f);
            t = new Date().getTime() - t;
            System.out.println(
                "Using a String took " + t + " ms.");
        } finally {
            f.close();
        }
    }
}
Digital Puer - 22 Feb 2006 23:04 GMT
> OK, so I killed the String test.  It was slowing down my machine.

LOL. Point well understood. Thank you very much for the code.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.