java BufferedInputStream 的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3122422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Usage of BufferedInputStream
提问by Jason Watkins
Let me preface this post with a single caution. I am a total beginner when it comes to Java. I have been programming PHP on and off for a while, but I was ready to make a desktop application, so I decided to go with Java for various reasons.
让我以一个警告作为这篇文章的序言。说到 Java,我完全是初学者。我已经有一段时间断断续续地编写 PHP 了,但我已经准备好制作一个桌面应用程序,所以我出于各种原因决定使用 Java。
The application I am working on is in the beginning stages (less than 5 classes) and I need to read bytes from a local file. Typically, the files are currently less than 512kB (but may get larger in the future). Currently, I am using a FileInputStreamto read the file into three byte arrays, which perfectly satisfies my requirements. However, I have seen a BufferedInputStreammentioned, and was wondering if the way I am currently doing this is best, or if I should use a BufferedInputStreamas well.
我正在处理的应用程序处于开始阶段(少于 5 个类),我需要从本地文件中读取字节。通常,文件当前小于 512kB(但将来可能会变大)。目前,我使用 aFileInputStream将文件读入三个字节数组,完全满足我的要求。但是,我看到了一个BufferedInputStream提到的,并且想知道我目前这样做的方式是否最好,或者我是否也应该使用 a BufferedInputStream。
I have done some research and have read a few questions here on Stack Overflow, but I am still having troubles understanding the best situation for when to use and not use the BufferedInputStream. In my situation, the first array I read bytes into is only a few bytes (less than 20). If the data I receive is good in these bytes, then I read the rest of the file into two more byte arrays of varying size.
我已经做了一些研究,并在 Stack Overflow 上阅读了一些问题,但我仍然无法理解何时使用和不使用BufferedInputStream. 在我的情况下,我读入字节的第一个数组只有几个字节(小于 20)。如果我收到的数据在这些字节中是好的,那么我将文件的其余部分读入另外两个不同大小的字节数组。
I have also heard many people mention profiling to see which is more efficient in each specific case, however, I have no profiling experience and I'm not really sure where to start. I would love some suggestions on this as well.
我还听到很多人提到分析以查看在每种特定情况下哪种更有效,但是,我没有分析经验,我不确定从哪里开始。我也喜欢这方面的一些建议。
I'm sorry for such a long post, but I really want to learn and understand the best way to do these things. I always have a bad habit of second guessing my decisions, so I would love some feedback. Thanks!
我很抱歉发表这么长的帖子,但我真的很想学习和理解做这些事情的最佳方式。我总是有一个坏习惯,重新猜测我的决定,所以我希望得到一些反馈。谢谢!
回答by Stephen C
If you are consistently doing small reads then a BufferedInputStreamwill give you significantly better performance. Each read request on an unbuffered stream typically results in a system call to the operating system to read the requested number of bytes. The overhead of doing a system call is may be thousands of machine instructions per syscall. A buffered stream reduces this by doing one large read for (say) up to 8k bytes into an internal buffer, and then handing out bytes from that buffer. This can drastically reduce the number of system calls.
如果您一直在进行少量读取,那么 aBufferedInputStream将为您提供明显更好的性能。对无缓冲流的每个读取请求通常会导致对操作系统的系统调用以读取请求的字节数。执行系统调用的开销可能是每个系统调用数千条机器指令。缓冲流通过对内部缓冲区执行一次(比如说)高达 8k 字节的大读取,然后从该缓冲区分发字节来减少这种情况。这可以大大减少系统调用的数量。
However, if you are consistently doing large reads (e.g. 8k or more) then a BufferedInputStreamslows things. You typically don't reduce the number of syscalls, and the buffering introduces an extra data copying step.
但是,如果您一直在进行大量读取(例如 8k 或更多),那么 aBufferedInputStream会减慢速度。您通常不会减少系统调用的数量,并且缓冲会引入额外的数据复制步骤。
In your use-case (where you read a 20 byte chunk first then lots of large chunks) I'd say that using a BufferedInputStreamis more likely to reduce performance than increase it. But ultimately, it depends on the actual read patterns.
在您的用例中(首先读取 20 字节的块,然后读取大量的大块),我会说使用 aBufferedInputStream更有可能降低性能而不是提高性能。但最终,这取决于实际的读取模式。
回答by Tom Hawtin - tackline
If you are using a relatively large arrays to read the data a chunk at a time, then BufferedInputStreamwill just introduce a wasteful copy. (Remember, readdoes not necessarily read all of the array - you might want DataInputStream.readFully). Where BufferedInputStreamwins is when making lots of small reads.
如果您使用相对较大的数组一次读取一个块的数据,那么BufferedInputStream只会引入一个浪费的副本。(请记住,read不一定要读取所有数组 - 您可能需要DataInputStream.readFully)。成功的地方BufferedInputStream在于进行大量的小阅读。
回答by Jubal
BufferedInputStream reads more of the file that you need in advance. As I understand it, it's doing more work in advance, like, 1 big continous disk read vs doing many in a tight loop.
BufferedInputStream 会提前读取更多您需要的文件。据我了解,它提前做了更多的工作,例如,1 个大的连续磁盘读取与在紧密循环中执行许多操作。
As far as profiling - I like the profiler that's built into netbeans. It's really easy to get started with. :-)
就分析而言 - 我喜欢 netbeans 中内置的分析器。这真的很容易上手。:-)
回答by Jason McCreary
I can't speak to the profiling, but from my experience developing Java applications I find that using any of the buffer classes - BufferedInputStream, StringBuffer - my applications are exceptionally faster. Because of which, I use them even for the smallest files or string operation.
我无法谈论分析,但根据我开发 Java 应用程序的经验,我发现使用任何缓冲区类 - BufferedInputStream、StringBuffer - 我的应用程序都非常快。因此,我什至将它们用于最小的文件或字符串操作。
回答by Octavians
import java.io.*;
class BufferedInputStream
{
public static void main(String arg[])throws IOException
{
FileInputStream fin=new FileInputStream("abc.txt");
BufferedInputStream bis=new BufferedInputStream(fin);
int size=bis.available();
while(true)
{
int x=bis.read(fin);
if(x==-1)
{
bis.mark(size);
System.out.println((char)x);
}
}
bis.reset();
while(true)
{
int x=bis.read();
if(x==-1)
{
break;
System.out.println((char)x);
}
}
}
}

