| | |
Reading in a text file -- efficiently
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 1,563
Reputation:
Solved Threads: 196
I had a problem w/ efficiency - The problem was that using String concatenation was inefficient for large text files, when reading the whole file into the String. I've since thought about it, and I guess this is because Strings are immutable - so every time concatenation is done, a new String object has to be created and the underlying char array has to be copied? After some research, I came across StringBuilder and String___ (I forget the rest of the name of the class). My question is, how can a text file be read in as efficiently as possible? I don't really care what type its read into. Also, a lot of sources I found said StringBuilder was the best way, is that true?
I think StringBuffer might be the other String__ class you were referring to. Both the StringBuilder and StringBuffer class are better at concatenation than the base String class. StringBuilder will out-perform StringBuffer (although both are highly efficient at concatenation) but note that StringBuilder does not guarantee synchronisation, meaning that you should only use it if it is being accessed by a single Thread. Multi-Threaded applications need to use StringBuffer, or control the synchronisation themselves.
Last edited by darkagn; Dec 6th, 2008 at 7:05 pm.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. Terms like efficiency make little sense when used in an absolute context. For finding the most efficient way to solve a problem you first need to track down your applications' usage pattern i.e. what kind of file reading does your application need? Is the data read promptly consumed or is stored for future use? Profiling your application is by far the best approach to selecting an efficient solution.
BTW,
BTW,
StringBuilder & StringBuffer are mutable companion classes to the immutable class String ; one being non-thread safe and the other being thread safe respectively. I don't accept change; I don't deserve to live.
Well, if you're simply going to be reading the entire file into a single String without any kind of work on the lines read until after the entire file is read, I wouldn't use either of those, and wouldn't use a Reader at all. I would do it like this
This is, of course, missing all error handling. That's for you. This also assumes that encoding won't be problem.
You are not forced to use a Reader simply because what you're reading is a text file.
Java Syntax (Toggle Plain Text)
File f = new File("filename"); FileInputStream fis = new FileInputStream(f); byte[] b = new byte[f.length]; int read = 0; while (read < b.length) { read += fis.read(b, read, b.length - read); } String text = new String(b);
This is, of course, missing all error handling. That's for you. This also assumes that encoding won't be problem.
You are not forced to use a Reader simply because what you're reading is a text file.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
> This also assumes that encoding won't be problem.
If this is what he actually wants, encoding shouldn't be a problem as long as an appropriate character set is specified when creating the string.
>
Array bites? ;-)
If this is what he actually wants, encoding shouldn't be a problem as long as an appropriate character set is specified when creating the string.
String text = new String(bytes, "UTF-8"); .>
byte[] b = new byte[f.length]; .Array bites? ;-)
byte[] bytes = new byte[f.length()]; I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- help (C++)
- delphi word (Pascal and Delphi)
Other Threads in the Java Forum
- Previous Thread: Modifying a file.
- Next Thread: array index and returning
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list login loops mac map method methods mobile netbeans notdisplaying number online printf problem program programming project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






