Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Text File Writing Speed

843789Jul 22 2009 — edited May 19 2010
Asking two questions in one day!

I am splitting one text file into three with quite a lot of data ---- 800 megs or so.

I had worked out some formulas in my program to determine the maximum number of lines that could be read in from the first text file and written at a time, since it had to be done in chunks. I was using the default memory settings at first. I thought that by increasing the number of lines that could be read at one time I might increase the speed that the files were written, so I increased the memory to 512megs and multiplied the number of lines in each chunk by 8, but it actually runs slower.

It took 72.265 seconds to run at 1x and 108.093 to run at 8x. Decreasing to 1/2X and 1/4X did not increase speed. Why is 8X the number of lines at a time so much slower? Should be the same number of method calls if I know my program well enough. A good question to ask is, if it is the same number of method calls, why did I think it would speed it up?

Is there a way to speed up text file writing? I already concatenate the tokens I want to write into one line so that each line requires one call to the PrintWriter. Should I try to concatenate multiple lines with the newline character to reduce PrintWriter calls? Should I be using something other than PrintWriter? Are there other things I can do?

If I want to concatenate an array of Strings into one String, how can I initialize the String to be the right size. I mean, a String is an array of characters, right? I don't want to be expanding the size of an array thousands of times as I concatenate, but rather just add to an array I have initialized to the right size. I looked at the constructors for String, like String(byte[] bytes) but I don't quite understand how I would use it.

Thanks
--John

Edited by: John_Sparger on Jul 22, 2009 11:33 AM

Edited by: John_Sparger on Jul 22, 2009 11:37 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 16 2010
Added on Jul 22 2009
32 comments
1,406 views