|
Re: Formatter and Appendable ?
|
Posted: Jul 11, 2005 11:22 PM
|
|
Hi there:
How large a file are we talking of?
There maybe alternatives in the Java API, but as we don't know their implementations, they might also be using StringBuffers.
Using a StringBuffer in the algorithm is the fastest way *I can* think of - at least just off the top of my head. Its definitely quicker than using constantly reading and appending it via a String.
Actually, I think the algorithm should be fairly efficient with respect to speed (you can always test it against other implementations via timestamps).
Only thing is only spit it back out as a String (toString()) when you are either finally reading to or from the file.
Check out this:
String globalString;// initiated in Constructor
// Expensive method public String app(String str){ globalString = globalString+str; }
// supposing you want to append a few hundred for(int i = 0; i < myStrings.length; i++){ app(myStrings[i]); } // In expensive method public String app(String str){ myStringBuf.append(str); } // using buffer algorithm for(int i = 0; i < myStrings.length; i++){ app(myStrings[i]); }
From an algorithm stand point the former recopies the String globalString each time before appending, whilst the latter simply keeps a String buffer and continuously appends a String to the String attribute in the buffer.
Hence I'm quite confident that it should be fairly efficient...
I hope this makes sense...
As I said this was the most efficient algorithm I could think of at the time (there are obviously others), but this should do the trick even on fairly large files (I think) - how large are the files you are dealing with?
Or I think you can try opening a File and get to EOF (without copying), then stick the String there.
Various options to explore.
Hopefully, if the files are extremely big you won't run into any "OutOfMemoryError" issues (in which case, research the EOF option I suggested), I'll post a snippet if I can think of one - right now some multithreading stuff is driving me nutts.
Good luck again, and I hope it helps...
|
|