简体   繁体   English

如何获得有关Java流的完整图片?

[英]How can get a complete picture about streams in Java?

Whenever I have to work with topics like file-handling or socket-programming, I have to look for sample code on the internet to see how the xxStreamxx,xxReader,xxWriter classes are used. 每当需要处理诸如文件处理或套接字编程之类的主题时,就必须在Internet上查找示例代码,以了解如何使用xxStreamxx,xxReader,xxWriter类。 I want to be able to use them on my own and know how they work. 我希望能够自己使用它们并知道它们的工作方式。

How do I go about learning that? 我该如何学习呢?

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). 读取器/写入器用于字符读取/写入(在Java中,字节不是char,因为char是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. 例如, FileInputStream extends InputStream是ty读取文件的二进制文件。 If this is textual file to read, you wrap this object into InputStreamReader extends Reader providing character set. 如果这是要读取的文本文件,则将此对象包装到InputStreamReader extends Reader提供字符集。 Now you are able to read characters. 现在,您可以阅读字符了。

If you want to do readLine() you need to wrap this reader into BufferedReader . 如果要执行readLine() ,则需要将此阅读器包装到BufferedReader

Similarly with writers. 与作家相似。

So, the idea is wrapping to get new abilities. 因此,这个想法是为了获得新的能力而包装。

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/ 在此网站上,您可以找到有关如何用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. 请滚动到页面的中间和底部。

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 . 因此,请阅读“ java教程的基本I / O课程”: 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 . 关于此主题的全面介绍是来自javapassion.com的: http : //www.javapassion.com/javase/javaiostream.pdf

While reading, as I usually told my students: "write code and make mistakes" :-) 在阅读时,正如我通常告诉学生的那样:“编写代码并犯错误” :-)

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

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