Why is using BufferedInputStream to read a file byte by byte faster than using FileInputStream
2016-11-30 20:55
459 查看
refer to http://stackoverflow.com/questions/18600331/why-is-using-bufferedinputstream-to-read-a-file-byte-by-byte-faster-than-using-f In FileInputStream, the method read()reads a single byte. From the source code: /** * Reads a byte of data from this input stream. This method blocks * if no input is yet available. * * @return the next byte of data, or <code>-1</code> if the end of the * file is reached. * @exception IOException if an I/O error occurs. */ public native int read() throws IOException; This is a native call to the OS which uses the disk to read the single byte. This is a heavy operation. With a BufferedInputStream, the method delegates to an overloaded read()method that reads 8192amount of bytes and buffers them until they are needed. It still returns only the single byte (but keeps the others in reserve). This way the BufferedInputStreammakes less native calls to the OS to read from the file. For example, your file is 32768bytes long. To get all the bytes in memory with a FileInputStream, you will require 32768native calls to the OS. With a BufferedInputStream, you will only require 4, regardless of the number of read()calls you will do (still 32768). As to how to make it faster, you might want to consider Java 7's NIO FileChannelclass, but I have no evidence to support this. |
| Aah I see, I should have checked the API first before asking. So it's simply an 8K internal buffer. That makes sense. Thanks. As for the "more efficient" part, it's not necessary, but I thought my code might have been overly redundant in some way. I guess it's not. – ZimZim Sep 3 '13 at 21:59 | ||||
| @user1007059 You're welcome. Note that if you usedFileInputStream's read(byte[], int, int)method directly instead, with a byte[>8192]you wouldn't need a BufferedInputStreamwrapping it. – Sotirios Delimanolis Sep 4 '13 at 4:01 | ||||
@SotiriosDelimanolis When to useread()byte by byte and when to use read(byte[])array of byte. As I think reading array is always better. then can you give me example where to use read()byte by byte OR read(byte[])array of byte. OR BufferedInputStream.? – UnKnown Apr 1 at 13:47 | |||||
@UnKnown Don't have a great example. Maybe the first byte contains some flag about the content of the file or some other metadata. I don't think anyone would ever read an entire file using read(). – Sotirios Delimanolis Apr 27 at 19:04 | |||||
FileChannel read and write are faster than any other approach.github.com/RedGreenCode/UVa/blob/master/Performance/… – Harish Sep 26 at 2:22 |
相关文章推荐
- How to read file in Java – BufferedInputStream Example
- 001:Why is it faster to process a sorted array than an unsorted array?
- JAVA常见问题其一:Why is it faster to process a sorted array than an unsorted array?
- How to avoid Excel from prompting "The file you are trying to open, , is in a different format than specified by the file extension"? (转)
- Why is it faster to process a sorted array than an unsorted array?
- Step By Step guide to Read XML file in Java Using SAX Parser Example
- FileInputStream RandomAccessFile BufferedReader MappedByteBuffer 大文件数据读取效率测试
- How to add, modify, or delete registry subkeys and values by using a registration entries (.reg) file
- Why is truth() faster than bool()? Part II
- Nhibernate select N+1 (to avoid by using lazy loading which is by default)
- Why is error code -36 (FS_FILE_EXISTS) returned by FS_CreateDir just after memory card is removed
- How to determine who is logged on to a database by using Microsoft Jet UserRoster in Access 2000
- save file as byte array to DB using jdbctemplate
- How to read file line by line?
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- Adpatch seems to hang when adworker is creating Database object using xdf file
- HOW TO: Upload a File to a Web Server in ASP.NET by Using Visual Basic .NET
- The installer cannot read the mwinstall.dll file, This is probably due to a CD reader which can.....
- The installer cannot read the mwinstall.dll file, This is probably due to a CD reader which can only read files with an eight.th
- Copy file to output by using attribute: DeploymentItem