即使您不传递Java扫描程序,它是否也会隐式创建一个缓冲区? [英] Does a Java Scanner implicitly create a buffer even if you do not pass it one?

查看:53
本文介绍了即使您不传递Java扫描程序,它是否也会隐式创建一个缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下示例文件,其中每个数字代表一个字节(123具有字节1、2和3):

If I have the following example file where each number is represents a byte (123 has bytes 1, 2, and 3):

123456789

假设我创建了FileInputStream。这将逐字节读取二进制文件。因此,.read()返回1,然后返回2,依此类推。现在,假设我创建了一个缓冲区。它读取的初始块(如果我正确理解缓冲区)为1-5。这样,它不仅可以逐字节读取字符,而且还可以整行读取字符等。但是,如果我再次点击.read(),我将从6开始,而不是从BufferedReader停止的地方开始(因此如果3是一个换行,我告诉BufferedReader打印第一行,它打印1-2,然后使用FileInputStream中的.read()给我6,而不是3。)

Let's say I create a FileInputStream. This reads in the binary byte by byte. So .read() returns 1, then 2, etc. Now let's say I create a buffer. The initial chunk it reads in (if I understand buffers correctly) is 1-5. This allows it to not only read in byte by byte, but in the case of characters whole lines, etc. But if I hit .read() again, I start at 6, and NOT where the BufferedReader stopped (so if 3 is a line break, and I told the BufferedReader to print the first line, it prints 1-2, and then using .read() from the FileInputStream gives me 6, and not 3.)

为了能够通过定界符解析数据,扫描程序是否隐式创建缓冲区,如BufferedReader创建缓冲区的方式一样,以便它可以找到换行符等?而且,如果我将单独的FileInputStream传递到Scanner中,则使用.read()不会在找到的扫描仪的第一个定界符之后打印第一个字节,而是在扫描仪拍摄的块末尾打印?

In order to be able to parse data by the delimiter, does a Scanner implicitly create a buffer like how a BufferedReader creates a buffer so that it can find line breaks, etc? And if I pass a separate FileInputStream into the Scanner, using .read() will NOT print the first byte following the first delimiter the scanner found, but rather at the end of the "chunk" the scanner took?

推荐答案

来自 java.util.Scanner 代码:

// Internal buffer used to hold input
private CharBuffer buf;

// Size of internal character buffer
private static final int BUFFER_SIZE = 1024; // change to 1024;

这篇关于即使您不传递Java扫描程序,它是否也会隐式创建一个缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆