简体   繁体   English

为什么我们在Java中将InputBytes写入OutputStream并从InputStream读取readLine?

[英]Why we writeBytes into a OutputStream and readLine out of a InputStream in java?

Here's just this example: 这只是这个例子:

http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139 http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139

Why it feels so strange? 为什么感觉如此奇怪?

You are actually looking at two different kinds of stream. 您实际上正在查看两种不同的流。

The Writer / Reader classes and subclasses are for reading / writing character-based data. Writer / Reader类和子类用于读取/写入基于字符的数据。 It takes care of conversion between Java's internal UTF-16 representation of text and the character encoding used outside. 它负责在Java的内部UTF-16文本表示和外部使用的字符编码之间进行转换。 The BufferedReader class adds a readLine() method that understands end-of-line makers. BufferedReader类添加了一个readLine()方法,该方法可以了解行尾制造商。

The InputStream / OutputStream classes and subclasses are for reading and writing byte-based data without any assumptions about character encodings, or that the data is text. InputStream / OutputStream类和子类用于读取和写入基于字节的数据,而无需任何有关字符编码的假设,或者该数据是文本。 Since it eschews these assumptions, "line" has no clear meaning, and hence the BufferedInputStream class does not have a readLine() method. 因为它避开了这些假设,所以“行”没有明确的含义,因此BufferedInputStream类没有readLine()方法。

(Incidentally, DataInputStream does have a readLine() method, but it is deprecated because it is broken. It makes assumptions about encodings, etc that are invalid on some platforms!) (顺便说一句, DataInputStream确实具有readLine()方法,但由于它已损坏而已弃用。它对编码等进行的假设在某些平台上是无效的!)


In your particular example, the code is asymmetric because the HTTP service it designed to talk to is asymmetric. 在您的特定示例中,代码是不对称的,因为它设计用来与之通信的HTTP服务是不对称的。 The service expects a request with binary content (encoded using the DataOutputStream wrapper), and delivers a response with text content. 该服务期望一个带有二进制内容的请求(使用DataOutputStream包装器编码),并提供一个带有文本内容的响应。 This is not particularly unusual ... or wrong. 这不是特别不寻常……或错误。

The strangeness of writing the "input" to a server to an "output" is merely a matter of perspective. 将服务器的“输入”写入“输出”的奇怪之处只是一个视角问题。 In simple terms, an OutputStream / Writer is something you "write to" (ie a data sink) and an InputStream or Reader is something you "read from" (ie a data source). 简而言之,OutputStream / Writer是您“写入”的内容(即数据接收器),而InputStream或Reader是您“读取”的内容(即数据源)。 That's just the way it is, and it is not strange at all once you get used to it. 就是这样,一旦习惯了就一点也不奇怪。

Actually, we don't. 实际上,我们没有。 There is no method readLine defined in InputStream . InputStream中没有定义方法readLine。 It also operates on bytes only, just like OutputStream. 它也仅对字节操作,就像OutputStream一样。

In the code you referenced, readLine is called on a BufferedReader . 在您引用的代码中,在BufferedReader上调用readLine。

Reader and Writer are for text data and operate on characters (and Strings), InputStream and OutputStream work with binary data (raw bytes). Reader和Writer用于文本数据,并且对字符(和字符串)进行操作,InputStream和OutputStream与二进制数据(原始字节)一起使用。 To convert between the two (ie wrap an InputStream into a Reader or an OutputStream into a Writer), you need to choose a character set. 要在两者之间进行转换(即将InputStream封装为Reader或将OutputStream封装为Writer),您需要选择一个字符集。

I'm feeling strange why not read out from OutputStream but from InputStream 我很奇怪为什么不从OutputStream而是从InputStream读出

That's just a matter of perspective. 这只是一个观点问题。

An OutputStream or a Writer is where you write your output to. 您可以将OutputStreamWriter 写入输出

An InputStream or a Reader is where you read your input from. 您可以InputStreamReader读取输入

Of course, somewhere, on the other end of the stream, someone might treat your OutputStream as their InputStream ... 当然,在流的另一端,某人可能会将您的OutputStream视为其InputStream ...

readLine does exactly what the name implies -- it reads a line of text until the end-of-line marker. readLine确实符合其名称的含义-读取一行文本,直到行尾标记。

When you write to a stream, you already know where your line ends. 写入流时,您已经知道行的结尾。

If you are looking for a way to write to streams in a more intuitive way, try PrintWriter . 如果您正在寻找一种以更直观的方式写入流的方法,请尝试使用PrintWriter

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

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