下面四张图表明了类之间的继承关系,其中红色、加粗的类名是常用的类。

常用转换

FileReader——>BufferedReader

BufferedReader in= new BufferedReader(new FileReader("Text.java"));

InputStream——>InputStreamReader——>BufferedReader

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

String——>byte[]——>ByteArrayInputStream——>DataInputStream

DataInputStream in= new DataInputStream(new ByteArrayInputStream(str.getBytes()));

FileInputStream——>BufferedInputStream——>DataInputStream

DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));

FileWriter——>BufferedWriter——>PrintWriter

PrintWriter pw=new PrintWriter(new BufferedWriter("text.out"));

System.out(PrintStream)——>PrintWriter

PrintWriter pw=new PrintWriter(System.out,true);

FileOutputStream——>BufferedOutputStream——>PrintStream

PrintStream ps= new PrintStream(new BufferedOutputStream(new FileOutputStream("text.out")));

FileOutputStream——>BufferedOutputStream——>DataOutputStream

DataOutputStream dos= new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));

程序举例

import java.io.*;

public class IOStreamDemo {

    public static void main(String[] args) throws IOException {

    /* 1.要想打开文件读取字符,你得先用String或File对象创建一个FileReader。
为了提高速度,你应该对这个文件作缓冲,因此你得把FileReader的reference 交给BufferedReader。
BufferedReader提供了readLine()方法,当你读到文件的末尾时readLine()会返回一个null,于是就退出while()循环了。
String sb是用来累加文件内容的,(再加一个换行符“\n”因为readLine()会把它们都剥掉).
最后用close()来清空缓冲区。*/
String s = "test";
StringBuilder source = new StringBuilder();
source.append(s);
source.append(System.getProperty("line.separator")); /* 2. 用System.in生成一个能读取控制台输入的流。System.in是一个InputStream,
而BufferedReader需要一个Reader作参数,所以要先通过InputStreamReader来转转手。
Java遵循标准I/O的模型,提供了Syetem.in,System.out,以及System.err。
System.out是一个已经预先处理过的,被包装成PrintStream的对象。
System.err也是一个PrintStream;
System.in是一个未经处理的InputStream。
也就是说,虽然你可以直接往System.out和System.err上写,但是要想读System.in的话,就必须先做处理了。*/
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a line:");
System.out.println(reader.readLine()); /* 3. 用String s2作参数创建一个StringReader。然后用StringReader的read()方法把字符读出来,再送到控制台上去。
read()会把读出来的byte当作int,所以要想正常打印的话,你得先把它们转成char。*/
StringReader stringReader = new StringReader(source.toString());
int c;
while ((c = stringReader.read()) != -1)
System.out.print((char) c); /*
4. 要想读取"格式化"的数据,你就得用DataInputStream了,DataInputStream是一个面向byte的I/O类,不是面向char的。
因此你只能从头到底一直用InputStream了。
当然你可以把所有的东西都当成byte。
然后用InputStream读出来。
但这里是String。要想把String当成byte数组。可以用String的getBytes()方法。而ByteArrayInputStream是可以处理byte数组的。
*/ System.out.println("===============ByteArrayInputStream begin===============");
DataInputStream byteArrayInputStream = new DataInputStream(
new ByteArrayInputStream(source.toString().getBytes()));
byte[] bytes = new byte[100];
while ((byteArrayInputStream.read(bytes)) != -1) {
System.out.print(new String(bytes));
}
System.out.println(System.getProperty("line.separator"));
System.out.println("===============ByteArrayInputStream end==============="); /* 5.向文件中写数据。先创建一个FileWriter,BufferedWriter是免不掉的。
然后再让PrintWriter去排版。这样就能得出能够读得懂的,普通的文本文件了。
输入流用完之后,readLine()会返回null。
最后调用close()方法清空缓冲区。*/ BufferedReader bufferedReader = new BufferedReader(new StringReader(source.toString()));
PrintWriter printWriter = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
int lineCount = 1;
while ((s = bufferedReader.readLine()) != null)
printWriter.println(lineCount++ + ": " + s);
printWriter.close(); DataOutputStream dataOutputStream = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream("Data.txt"))); /*6. 存储和恢复数据
PrintWriter会对数据进行格式化,这样人就能读懂了。
但是如果数据输出之后,还要恢复出来供其它流用,那你就必须用DataOutputStream来写数据,再用DataInputStream来读数据了。
当然,它们可以是任何流,不过我们这里用的是一个经缓冲的文件。
DataOutputStream和DataInputStream是面向byte的,因此这些流必须都是InputStream和OutputStream。
如果数据是用DataOutputStream写的,那么不管在哪个平台上,DataInputStream都能准确地把它还原出来。
这一点真是太有用了,因为没人知道谁在为平台专属的数据操心。如果你在两个平台上都用Java,那这个问题就根本不存在了。
用DataOutputStream写String的时候,要想确保将来能用DataInputStream恢复出来,唯一的办法就是使用UTF-8编码。
UTF-8是Unicode的一种变形。Unicode用两个字节来表示一个字符。
但是,如果你处理的全部,或主要是ASCII字符(只有7位),那么无论从存储空间还是从带宽上看,就都显得太浪费了,
所以UTF-8 用一个字节表示ASCII字符,用两或三个字节表示非ASCII的字符。
此外,字符串的长度信息存在(字符串)的头两个字节里。writeUTF( )和readUTF( )用的是Java自己的UTF-8版本(JDK文档里有关于这个字符集的完整讲解,
就在这两个方法的文档里),
所以如果你要用一个Java程序读取writeUTF( )写的字符串的话,就必须进行一些特殊处理了。
有了writeUTF( )和readUTF( ),你就能放心地把String和其它数据混在一起交给DataOutputStream了,
因为你知道String是以Unicode的形式存储的,而且可以很方便地用DataOutputStream恢复出来。
writeDouble( )会往流里写double,而它"影子"readDouble( )则负责把它恢复出来(其它数据也有类似的读写方法)。
但是要想让读取方法能正常工作,你就必须知道流的各个位置上都放了些什么数据。因为你完全可以把double读成byte,char,或其它什么东西。
所以要么以固定的格式写文件,要么在文件里提供额外的解释信息,然后一边读数据一边找数据。先提一下,对于复杂数据的存储和恢复,对象的序列化可能会比较简单。*/
dataOutputStream.writeDouble(3.14159);
dataOutputStream.writeUTF("That was pi");
dataOutputStream.writeDouble(1.41413);
dataOutputStream.writeUTF("Square root of 2");
dataOutputStream.close();
DataInputStream dataInputStream = new DataInputStream(
new BufferedInputStream(new FileInputStream("Data.txt")));
System.out.println(dataInputStream.readDouble());
System.out.println(dataInputStream.readUTF());
System.out.println(dataInputStream.readDouble());
System.out.println(dataInputStream.readUTF()); }
}

http://blog.sina.com.cn/s/blog_4cc16fc50100bvyb.html

InputStream/OutputStream: 
1)抽象类,2)面向字节形式的I/O操作(8 位字节流) 。 
Reader/Writer: 
1)抽象类,2)面向字符的 I/O操作(16 位的Unicode字符) 。 
InputStreamReader: 
可以将InputStream转换为 Reader。 
OutputStreamWriter: 
可以将OutputStream转换为Writer。

Java I/O的核心采用了Decorator(装饰)模式。

http://wentao365.iteye.com/blog/1183951

最新文章

  1. ASP.NET MVC5+EF6+EasyUI 后台管理系统(56)-插件---单文件上传与easyui使用fancybox
  2. google你懂得
  3. topcoder SRM 610 DIV2 DivideByZero
  4. Aspose.Word 操作word表格的行 插入行 添加行
  5. Easyui主要组件用法
  6. window.open窗口关闭后刷新父窗口代码
  7. c#操作XML文件的通用方法
  8. echarts的使用总结;
  9. centos 安装cacti监控
  10. Redis 小白指南(一)- 简介、安装、GUI 和 C# 驱动介绍
  11. PHP开发中需要注意几点事项,新手少走弯路必备知识
  12. android新窗口以及传值
  13. Opencv——相机标定
  14. CSS的初了解(更新中···)
  15. Python-random 随机数模块
  16. MySQL的安装与维护
  17. 【转】每天一个linux命令目录
  18. eclipse下properties配置文件中文乱码解决
  19. BZOJ5297 CQOI2018 社交网络 【矩阵树定理Matrix-Tree】
  20. easyui学习笔记1—增删改操作

热门文章

  1. map循环
  2. MySQL恢复备份读书笔记
  3. Amazon MWS 上传数据 (一) 设置服务
  4. Robot Framework语法学习(一)
  5. win8 VS2010 配制OpenGL
  6. wetask.cn领度任务全新试用体验
  7. 用sql语句按周、按月、按季、按年统
  8. poj2924---高斯求和
  9. 设计模式六大原则——迪米特法则(LoD)
  10. 用事件与CSS改变按钮不同状态下的颜色