如何全面了解 Java 中的流?

发布于 2025-01-04 10:58:28 字数 1540 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与风相奔跑 2025-01-11 10:58:28

主要思想很简单。

流用于二进制读/写。读取器/写入器用于字符读取/写入(在 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 into InputStreamReader extends Reader providing character set. Now you are able to read characters.

If you want to do readLine() you need to wrap this reader into BufferedReader.

Similarly with writers.

So, the idea is wrapping to get new abilities.

豆芽 2025-01-11 10:58:28

首先,您必须学习并理解什么是流。如果你不理解它们背后的概念,你将总是在复制代码。所以请阅读“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" :-)

小梨窩很甜 2025-01-11 10:58:28

在此网站中,您可以找到有关如何用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文