From: Mark on
On Thu, 21 Jan 2010 05:20:07 -0800, Roedy Green
<see_website(a)mindprod.com.invalid> wrote:

>On Thu, 21 Jan 2010 10:31:04 +0000, Mark
><i(a)dontgetlotsofspamanymore.invalid> wrote, quoted or indirectly
>quoted someone who said :
>
>> text += (char)c;
>
>use a StringBuilder to accumulate the text. Guess a generous starting
>size.
>
>see http://mindprod.com/jgloss/stringbuilder.html

Thanks. That's much faster.
--
(\__/) M.
(='.'=) Due to the amount of spam posted via googlegroups and
(")_(") their inaction to the problem. I am blocking most articles
posted from there. If you wish your postings to be seen by
everyone you will need use a different method of posting.
[Reply-to address valid until it is spammed.]

From: Knute Johnson on
On 1/21/2010 2:31 AM, Mark wrote:
> Hi,
>
> I am using a BufferedReader to read character data in from a file. It
> works but it's incredibly slow. (The file consists of a number of
> separate messages, each separated by a special character. Each
> message must be read into a separate string.)
>
> I use the following code (exception handling removed for brevity):
>
> String text = new String("");
> BufferedReader in = null;
> in = new BufferedReader(new InputStreamReader(new
> FileInputStream(_msgFile)));
> int c;
> while ((c = in.read()) != -1) {
> if (c == '@') {
> _msgList.add(text);
> text = "";
> } else {
> text += (char)c;
> }
> }
> if (text.length()> 0) {
> _msgList.add(text);
> }
>

I would go at this a little differently. Use a BufferedInputStream with
a very large buffer and pass it to a Scanner. That way you can pull off
the Strings directly.

--

Knute Johnson
email s/nospam/knute2010/

From: Lew on
Mark wrote:
> I am using a BufferedReader to read character data in from a file.  It
> works but it's incredibly slow.  (The file consists of a number of
> separate messages, each separated by a special character.  Each
> message must be read into a separate string.)
>

Aside from the good answers others have provided, some additional side
remarks:

> I use the following code (exception handling removed for brevity):

Most of the example removed for too much brevity.

>             String text = new String("");

Why not 'text = ""'?

>             BufferedReader in = null;

Why in the world would you assign to 'null' first, then discard that
initialization immediately?

>             in = new BufferedReader(new InputStreamReader(new
> FileInputStream(_msgFile)));
>             int c;

Put 'c' in the loop body; its scope is too wide here. Actually,
follow others' advice here and don't read the input character by
character but buffer by buffer, as Roedy said.

>             while ((c = in.read()) != -1) {
>                 if (c == '@') {
>                     _msgList.add(text);

We'll assume an appropriate definition for '_msgList', but notice that
the variable name violates the Java coding conventions.

>                     text = "";
>                 } else {
>                     text += (char)c;

Yes, Roedy's advice to use 'StringBuilder' will speed this up
considerably, and save heap memory.

>                 }
>             }
>             if (text.length() > 0) {
>                 _msgList.add(text);
>             }
>

<http://sscce.org/>

--
Lew

From: GK on
On Jan 21, 11:31 am, Mark <i...(a)dontgetlotsofspamanymore.invalid>
wrote:

> I use the following code (exception handling removed for brevity):
> String text = new String("");

better use: String text = "";
--> your code constructs the empty string and then constructs a new
empty string using the empty string.

@Roedy: you suggesting using StringBuilder which is never wrong, but
AFAIK later java releases do that optimization during compile.

Gernot
From: Lew on
Mark wrote:
>> I use the following code (exception handling removed for brevity):
>> String text = new String("");

GK wrote:
> better use: String text = "";
> --> your code constructs the empty string and then constructs a new
> empty string using the empty string.

Nope. The compiler and class initialization construct the empty string,
interning it into the string pool. That code line at execution then
constructs a new empty string based on the already existing interned empty string.

> @Roedy: you suggesting using StringBuilder which is never wrong, but
> AFAIK later java [sic] releases do that optimization during compile.

Not in the scenario of the OP's code:
> text += (char)c;

Each iteration through the loop creates a new 'String' and assigns it to 'text'.

--
Lew