[英]Reading with InputStream while using Scanner
I want to read from a client's inputstream
in two different ways: 我想以两种不同的方式从客户端的输入
inputstream
中读取:
1: Use Scanner
over the inputstream
to read strings. 1:在输入
inputstream
上使用Scanner
读取字符串。
2: Use the InputStream
object itself to read a file with a buffer. 2:使用
InputStream
对象本身读取带有缓冲区的文件。
At first, I want to read a text, that gives me the name and size of the File, in my case: "file:name:...", "file:size:...". 首先,我想阅读一个文本,该文本为我提供了File的名称和大小,在我的情况下为:“ file:name:...”,“ file:size:...”。
When I have this information, the InputStream
should actually read the file. 当我获得此信息时,
InputStream
应该实际读取该文件。 The problem is, that I try to read the file in the " scanner.hasNextLine()
"-loop with the InputStream
object but this causes the InputStream.read()
method to return -1 or EOS. 问题是,我尝试使用
InputStream
对象读取“ scanner.hasNextLine()
”循环中的文件,但这会导致InputStream.read()
方法返回-1或EOS。 I have brought the problem to a minimal size so you just have to answer me this question: 我已将问题缩小到最小,因此您只需要回答以下问题即可:
Why does the read-method return -1 here: 为什么读取方法在这里返回-1:
if (scanner.hasNextLine()) {
System.out.println(inputstream.read());
}
As the Oracle Documentation for Scanner.hasNextLine()
states, this method does not advance the Scanner
. 正如Oracle
Scanner.hasNextLine()
文档所述,此方法不会使Scanner
前进。 This means that you could be perpetually looking at the first line of your file with your Scanner
, while advancing your InputStream
. 这意味着您可以在前进
InputStream
同时,用Scanner
永久查看文件的第一行。 This means that even though the Scanner
has a next line, the InputStream
may not. 这意味着即使
Scanner
有下一行, InputStream
也可能没有。 You appear to be walking off the end of the file with the InputStream
. 您似乎正在使用
InputStream
走出文件结尾。
If you do not advance the Scanner
along with the input stream, you cannot know that the Scanner
is looking at the same location in the file. 如果不将
Scanner
与输入流一起前进,则无法知道Scanner
正在查找文件中的相同位置。 It is dubious at best to read one file simultaneously with a Scanner
and an InputStream
anyway. 无论如何最多只能同时使用
Scanner
和InputStream
读取一个文件是可疑的。
Consider refactoring your code to only use one or the other. 考虑将代码重构为仅使用其中一个。
If you would like to read the entire contents of the file into a single String
, consider this: 如果要将文件的全部内容读入单个
String
,请考虑以下事项:
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
and other methods from How do I create a Java string from the contents of a file? 如何从文件内容创建Java字符串?
Okay I don't need Scanner
and InputStream
anymore. 好的,我不再需要
Scanner
和 InputStream
了。 I can do both, sending texts and files, with the DataOutputStream
(with writeUTF(String)
and write(byte[], int, int)
). 我可以通过
DataOutputStream
(使用writeUTF(String)
和write(byte[], int, int)
)发送文本和文件。 This really works very well and simplifies my code a lot. 这确实非常有效,并且大大简化了我的代码。
Don't say this is dobious! 不要说这太可疑了! If you have a
Socket
, sometimes you need to use the InputStream
in two or three different ways. 如果您拥有
Socket
,有时您需要以两种或三种不同的方式使用InputStream
。 And I didn't want to use two Sockets. 而且我不想使用两个套接字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.