Java File I/O :Reader/Writer and when a Stream
来源:互联网 发布:如何推广淘宝网店 编辑:程序博客网 时间:2024/05/01 13:35
1. XXXInputStream and XXXOutputStream (where XXX varies, there's a lot of options) deal with 8-bit bytes. For example,OutputStream.write(byte[] c);
2. XXXWriter or XXXReader deal with 16-bit chars. For example, Reader.read(char[] cbuf)
.
3. OutputStreamWriter converts an OutputStream to a Writer.
4. InputStreamReader converts from an InputStream to a Reader.
5.FileWriter is a Writer that talks to files.
Since a Java String internally uses chars (16 bit so they can handle Unicode), FileWriter is the natural class for use with Unicode Strings.
6. FileOutputStream is an OutputStream for writing bytes to a file. OutputStreams do not accept chars (or Strings).
By wrapping it in an OutputStreamWriter you now have a Writer, which does accept Strings.
Now, the real question, is when do you use a Reader/Writer and when a Stream? I've used Java for years and sometimes I get confused too. I believe the following to be correct:
- If you are dealing with binary data (e.g. an image) use Streams.
- If you are using non-ASCII Unicode characters, e.g. Chinese, useReaders/Writers.
- If you are using ordinary ASCII text (the traditional 0-127 characters) you can (usually) use either
other references :
http://www.vogella.com/articles/JavaIO/article.html
http://stackoverflow.com/questions/4576222/fastest-way-to-write-to-file
inputstream and reader in Java IO
InputStream vs InputStreamReader
- Java File I/O :Reader/Writer and when a Stream
- Java I/O Reader and Writer
- Java I/O流-总结(InputStream,OutputStream,Reader,Writer)
- Java I/O流-总结(InputStream,OutputStream,Reader,Writer)
- Java I/O流-总结(InputStream,OutputStream,Reader,Writer)
- Java I/O 学习笔记(4) reader和writer
- Java I/O流InputStream,OutputStream,Reader,Writer
- 说说 Java I/O 系统之 Reader 与 Writer
- java-I/O File类(5)-Reader和Writer、OutputStreamWriter 、BufferedWriter、字节流和字符流的区别
- java-I/O File类(5)-Reader和Writer、OutputStreamWriter 、BufferedWriter、字节流和字符流的区别
- JavaSE I/O 字符流 Reader/Writer
- Java8 I/O源码-Reader与Writer
- Java IO reader and writer
- I/O流的设计(Java的InputStream/OuputStream和Reader/Writer的C++实现)
- Java 7之传统I/O - 字符接口 Reader和Writer
- Java I/O Stream
- Java 流Stream、文件File和I/O
- Stream I/O and Files
- 传入中文参数导致的一系列乱码问题
- c++ 函数后面的const理解
- Arcgis Server系列 ADF的开发与实例
- android MediaRecorder 录制音频、视频代码片段
- Java 程序员这个职业赚钱吗?能赚多少钱?
- Java File I/O :Reader/Writer and when a Stream
- [LeetCode] Validate Binary Search Tree
- VB.NET应用程序中多线程的应用实例
- windows下修改eclipse的默认编码 转
- html5+css3布局尝试
- leetcode 50: Valid Number
- Vmware 9安装Ubuntu 12.10 完毕后,没有图形界面(已解决)!
- 802.11 协议族
- qt画图程序总结