简体   繁体   English

使用Scanner时使用InputStream进行读取

[英]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. 无论如何最多只能同时使用ScannerInputStream读取一个文件是可疑的。

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.

相关问题 Java-使用Scanner从Socket InputStream读取 - Java - Using Scanner reading from Socket InputStream 多次使用扫描仪从输入流读取内容的行为与预期不符 - Reading the content from an inputstream using scanner multiple times is not behaving as expected 使用InputStream读取文本文件 - Reading a textfile using InputStream 如何使用httpurlconnection从InputStream读取时设置超时? - How to set a timeout while reading from InputStream using httpurlconnection? 读取InputStream时忽略编码 - Encoding ignored while reading InputStream 从InputStream读取时发生IOException - IOException while reading from InputStream 为什么 BufferedInputStream(InputStream in, int bufSize) 在使用 mark() 时没有从 inputStream 中读取 bufSize 块中的数据? - Why BufferedInputStream(InputStream in, int bufSize) is not reading data in chunks of bufSize from the inputStream while using mark()? 如何在Java中使用扫描仪读取.txt文件时删除inputMismatchException - how to remove inputMismatchException while reading a .txt file using scanner in java 使用扫描仪读取文件时出现异常 - Getting exception while reading from a file using Scanner 使用扫描仪读取一系列行时出现问题 - Getting issues while reading series of lines using scanner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM