简体   繁体   English

使用线程读取 Java 中的图像

[英]Reading an image in Java with threads

I have a question regarding reading images in Java.我有一个关于阅读 Java 中的图像的问题。 I am trying to read an image using threads and i was curious whether by doing this: myInputFile = new FileInputStream(myFile);我正在尝试使用线程读取图像,我很好奇是否这样做: myInputFile = new FileInputStream(myFile); I already read the whole data or not.我是否已经阅读了全部数据。 I already read it in 4 chunks using threads and i am curious whether I just read it twice, once with threads and once with FileInputStream , or what does FileInputStream exactly do.我已经使用线程以 4 个块的形式阅读了它,我很好奇我是否只阅读了两次,一次使用线程,一次使用FileInputStream ,或者FileInputStream究竟做了什么。 Thanks in advance!提前致谢!

The FileInputStream is not reading your file yet, just by calling it like: myInputFile = new FileInputStream(myFile); FileInputStream尚未读取您的文件,只需调用它: myInputFile = new FileInputStream(myFile); . .

It basically only gives you a handle to the underlying file and prepares to read from it by opening a connection to that file.它基本上只为您提供底层文件的句柄,并准备通过打开与该文件的连接来读取它。 Also it runs some basic checks including whether the file exists and if its a proper file and not a directory.它还运行一些基本检查,包括文件是否存在以及它是否是正确的文件而不是目录。

Following is stated in the JavaDocs which you can find here :您可以在此处找到的 JavaDocs 中说明了以下内容:

Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.通过打开与实际文件的连接来创建 FileInputStream,该文件由文件系统中的 File object 文件命名。 A new FileDescriptor object is created to represent this file connection.创建一个新的 FileDescriptor object 来表示这个文件连接。

First, if there is a security manager, its checkRead method is called with the path represented by the file argument as its argument.首先,如果存在安全管理器,则调用其 checkRead 方法,并将文件参数表示的路径作为其参数。

If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a FileNotFoundException is thrown.如果命名文件不存在,是目录而不是常规文件,或者由于某些其他原因无法打开读取,则抛出 FileNotFoundException。

Only by calling the FileInputStream.read methods it starts to read and return the contents of the file.只有通过调用FileInputStream.read方法,它才会开始读取并返回文件的内容。

Thereby the FileInputStream.read() method will only read one single byte of the file and the FileInputStream.read(byte[] b) method will read as many bytes as the size of the byte array b .因此FileInputStream.read()方法将只读取文件的一个字节,而FileInputStream.read(byte[] b)方法将读取与字节数组b的大小一样多的字节。

Edit:编辑:

Because reading a file byte by byte is pretty slow and the usage of the plain FileInputStream.read(byte[] b) method can be a bit cumbersome it's a good practice to use the BufferedInputStream to process files in Java.因为逐字节读取文件非常慢,并且使用普通的FileInputStream.read(byte[] b)方法可能有点麻烦,所以使用BufferedInputStream处理 Java 中的文件是一个好习惯。

It'll read by default the next 8192 bytes of a file and buffer it in-memory for faster access.默认情况下,它将读取文件的下一个 8192 字节并将其缓冲在内存中以便更快地访问。 So the BufferedInputStream.read method will still only return a single byte per call, but in the BufferedInputStream it'll mainly be served from an internal buffer.所以BufferedInputStream.read方法每次调用仍将只返回一个字节,但在BufferedInputStream中,它将主要从内部缓冲区提供服务。 As long the requested bytes are in this buffer, they'll be served from it.只要请求的字节在此缓冲区中,它们就会从中得到服务。 The underlying file will be accessed again only when really needed (-> the requested byte is not in the buffer anymore).只有在真正需要时才会再次访问底层文件(-> 请求的字节不再在缓冲区中)。 This drastically reduces the number of read accesses to the hardware (which in comparison is the slowest operation in this process) and therefore boosts the reading performance a lot.这大大减少了对硬件的读取访问次数(相比之下,这是此过程中最慢的操作),因此大大提高了读取性能。

The initialization looks like this:初始化如下所示:

InputStream i = new BufferedInputStream(new FileInputStream(myFile));

The handling of it is exactly same as with the 'plain' FileInputStream , since they share the same InputStream interface.它的处理与“普通” FileInputStream完全相同,因为它们共享相同的InputStream接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM