java InputStreamReader、OutputStreamWriter 的使用

1、

 1 package cn.kongxh.io4;
 2 
 3 import java.io.* ;
 4 public class InputStreamReaderDemo01{
 5     public static void main(String args[]) throws Exception{
 6         File f = new File("d:" + File.separator + "test.txt") ;    
 7         Reader reader = null ;
 8         reader = new InputStreamReader(new FileInputStream(f)) ;    // 将字节流变为字符流
 9         char c[] = new char[1024] ;
10         int len = reader.read(c) ;    // 读取
11         reader.close() ;    // 关闭
12         System.out.println(new String(c,0,len)) ;
13     }
14 };

2、

 1 package cn.kongxh.io4;
 2 
 3 import java.io.* ;
 4 public class OutputStreamWriterDemo01{
 5     public static void main(String args[]) throws Exception    {    // 所有异常抛出
 6         File f = new File("d:" + File.separator + "test.txt") ;    
 7         Writer out = null ;    // 字符输出流
 8         out = new OutputStreamWriter(new FileOutputStream(f)) ;    // 字节流变为字符流
 9         out.write("hello world!!") ;    // 使用字符流输出
10         out.close() ;
11     }
12 };

总结:

字节字符转换

写入数据

计算机-->(字符数据)---->字符流-->OutputStreamWriter-->字节流-->

程序---->内存中得数据---->将输出得字符流变为字节流--------------->文件-->

读入数据

计算机<--(字符数据)<----字符流<--InputStreamReader<--字节流<--

程序<----内存中得数据<----将输如得字节流变为字符流<---------------文件<--