We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 27 days ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
主要思想很简单。
流用于二进制读/写。读取器/写入器用于字符读取/写入(在 Java 中,字节不是 char,因为 char 是 unicode)。如果可以将二进制流转换为字符序列,那么就有适合流的读取器。
例如,
FileInputStream extends InputStream
是读取文件二进制文件。如果这是要读取的文本文件,则可以将此对象包装到提供字符集的InputStreamReader extends Reader
中。现在您可以读取字符了。如果您想做
readLine()
,您需要将此读取器包装到BufferedReader
中。作家也同样如此。
所以,我们的想法是通过包装来获得新的能力。
The main idea is simple.
Streams are for binary read/write. Readers/Writers are for character read/write (in Java byte is not a char, since char is unicode). If it is possible to convert binary stream into character sequence, there is an appropriate reader for a stream.
For example,
FileInputStream extends InputStream
is ty read file binary. If this is textual file to read, you wrap this object intoInputStreamReader extends Reader
providing character set. Now you are able to read characters.If you want to do
readLine()
you need to wrap this reader intoBufferedReader
.Similarly with writers.
So, the idea is wrapping to get new abilities.
首先,您必须学习并理解什么是流。如果你不理解它们背后的概念,你将总是在复制代码。所以请阅读“java教程的基础I/O课”: http: //docs.oracle.com/javase/tutorial/essential/io/streams.html。关于此主题的全面演示来自 javapassion.com:http://www.javapassion.com /javase/javaiostream.pdf。
在阅读时,正如我通常告诉我的学生:“编写代码并犯错误”:-)
First of all, you have to learn and understand what streams are. If you don't understand the concepts behind them, you will be always copying code. So read the "Basic I/O lesson of the java tutorial": http://docs.oracle.com/javase/tutorial/essential/io/streams.html. A comprehensive presentation about this topic is this one from javapassion.com: http://www.javapassion.com/javase/javaiostream.pdf.
While reading, as I usually told my students: "write code and make mistakes" :-)
在此网站中,您可以找到有关如何用 Java 编写自己的流的各种示例: http://java.sun.com/developer/technicalArticles/Streams/WritingIOSC/
仅查看示例有时比解释对您有更多帮助...
请滚动到中间和底部 页。
In this website you could find a variety of examples on how to write your own streams in Java: http://java.sun.com/developer/technicalArticles/Streams/WritingIOSC/
Just looking at the examples sometimes helps you much more than the explanations...
Please scroll to the middle and bottom of the page.