tag:blogger.com,1999:blog-8260739278874294486.post9006376620824868081..comments2021-12-16T15:28:31.042-08:00Comments on Tsuna's blog: Java IO: slowest readLine evertsunahttp://www.blogger.com/profile/06114951663056205324noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-8260739278874294486.post-82472825814421961272014-09-29T20:40:09.123-07:002014-09-29T20:40:09.123-07:00In case anyone was looking for a good solution usi...In case anyone was looking for a good solution using just the standard JDK...<br /><br />BufferedReader bufferedReader = new BufferedReader(new FileReader(file));<br />String line;<br />while ((line = bufferedReader.readLine()) != null)<br />{<br /> // process line<br />}<br />bufferedReader.close();Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8260739278874294486.post-49349681665103400622012-02-08T09:07:08.066-08:002012-02-08T09:07:08.066-08:00If you can help it, always use mapped files with D...If you can help it, always use mapped files with DirectBuffers. RandomAccessFile is retarded in reading all primitives, incl. readInt(), readLong(). Full JNI switch for each bytes, magic!!<br /><br />Of course mapping comes w/ a price of being unable ( to unmap (baring hacks)Stanimir Simeonoffhttps://www.blogger.com/profile/15526543718385237177noreply@blogger.comtag:blogger.com,1999:blog-8260739278874294486.post-26335864090766489962011-02-26T03:56:18.597-08:002011-02-26T03:56:18.597-08:00For any speedy file reading on Android you need to...For any speedy file reading on Android you need to build some class that uses an InputStream like DataInputStream then use chunked buffers and parse.<br /><br />Buffering the whole file obv gives you random access - into the buffer.<br /><br />If you have a size problem (most of us do! ;) ) then <br />Performant random access and seeking makes things more complicated.danmuxhttps://www.blogger.com/profile/01232356329701615766noreply@blogger.comtag:blogger.com,1999:blog-8260739278874294486.post-26147578274064621782011-01-05T23:09:39.759-08:002011-01-05T23:09:39.759-08:00you posted this on my birthday!
YO BEN! you in s...you posted this on my birthday!<br /><br /><br />YO BEN! you in sf now? come visit!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8260739278874294486.post-4955141182489323262010-12-15T17:23:57.806-08:002010-12-15T17:23:57.806-08:00@Anonymous: the javadoc of RandomAccessFile doesn&...@Anonymous: the javadoc of RandomAccessFile doesn't mention anything about reading byte by byte, it just says "This method successively reads bytes from the file". Either way, there's no reason whatsoever to have such an utterly stupid implementation of readLine. BufferedReader doesn't have a seek method. So you can either seek around or have a non-stupid readLine, but not both. Yay.tsunahttps://www.blogger.com/profile/06114951663056205324noreply@blogger.comtag:blogger.com,1999:blog-8260739278874294486.post-5890544646398323872010-12-15T15:22:28.229-08:002010-12-15T15:22:28.229-08:00And what about using java BufferReader, or read(by...And what about using java BufferReader, or read(byte[] b, int off, int len) if you need to use a RandomAccessFile ?<br />If I'm not wrong, javadocs tell us that it'll be read byte per byte, and might be inefficient...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8260739278874294486.post-80265891145568064052010-12-11T19:09:58.933-08:002010-12-11T19:09:58.933-08:00@Anonymous: you could do this if you know the file...@Anonymous: you could do this if you know the file isn't too large and can fit in memory – but still, you'd have to implement readLine yourself because the implementation in the JDK is unacceptable. In my case though, there's another reason why I require two passes through the file, the first pass pre-computes some data that the second pass needs.tsunahttps://www.blogger.com/profile/06114951663056205324noreply@blogger.comtag:blogger.com,1999:blog-8260739278874294486.post-37899122461441029392010-12-11T16:25:19.728-08:002010-12-11T16:25:19.728-08:00I'm still new to programming, but wouldn't...I'm still new to programming, but wouldn't it be better to use <br /><br />String s = "";<br />ArrayList a = new ArrayList();<br />while ((s=in.readline())!=null) a.add(s);<br /><br /><br />to get the entire file which you intend to read line-by-line anyway. Then you'd have a.size() as the number of lines in the file, and could use a for loop to access them quickly for whatever processing needs done. I don't know much about speed or memory usage yet, so please don't yell at me if I said something stupid. T.TAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8260739278874294486.post-25644414049083878132010-12-11T13:30:40.376-08:002010-12-11T13:30:40.376-08:00@royans: with a RandomAccessFile you can seek arou...@royans: with a RandomAccessFile you can seek around freely, no need to reopen the file.<br /><br />@Adrien: I always read the code of library functions before calling them from my code. I know not to trust the JDK especially, because there are a lot of dumb things in it. I spend a lot of time reading the code of the JDK on Google Code Search, but in this case I also double-checked that the JRE I was using had the problem with strace indeed. A number of packages in the JDK are very poorly implemented. Some packages are pretty awesome however, such as everything that Doug Lea wrote (no surprise there) for java.util.concurrent.tsunahttps://www.blogger.com/profile/06114951663056205324noreply@blogger.comtag:blogger.com,1999:blog-8260739278874294486.post-83043930483520880352010-12-11T09:17:45.048-08:002010-12-11T09:17:45.048-08:00Nice history !
How did you dive that deep into re...Nice history !<br /><br />How did you dive that deep into readLine() implementation ? Strace ?<br /><br />What about readFully() ? Did it also fill the byte array... byte after byte ?Anonymoushttps://www.blogger.com/profile/13887310572024815894noreply@blogger.comtag:blogger.com,1999:blog-8260739278874294486.post-2994213960140232322010-12-11T01:36:47.491-08:002010-12-11T01:36:47.491-08:00Wow, thats very interesting (and sort of dumb). Ho...Wow, thats very interesting (and sort of dumb). How expensive is to reopen the file if its huge file, or cache the contents of small files to avoid the "re-seeking start of file" issue.Flagthishttps://www.blogger.com/profile/16268344104057191288noreply@blogger.com