转换流: InputStreamReader 和 OutputStreamWriter

/*
* 1、转换流: InputStreamReader 和 OutputStreamWriter 一对。
*
* 1) InputStreamReader 字节转换成字符的一个输入流,最后,以字符方式读取数据。
* OutputStreamWriter 字符转换成字节的一个输出流,最后,以字节方式输出数据。
*
* 2) 功能: 当给的类型数据与要的类型数据不一致时,需要进行转换。
* 另一个功能: 可以为输入/输出指定字符编码集,从而以指定的字符编码集实现数据存盘管理或读取相应字符编码集的文件。
*
* 3) 案例1: 通过 FileInputStream类的对象去读取带汉字的文件内容并显示在屏幕时会乱码。
* 如何解决这个问题?
* 答案: 通过InputStreamReader转换流来实现。 或者更换FileInputStream为FileReader类实现读取。
*
* 4) 案例2:通过FileOutputStream类的对象把汉字存盘到文件中时会失真。
* 如何解决这个问题?
* 答案: 通过 OutputStreamWriter转换流来实现。 或者更换为FileWriter类实现存盘。
*
*/

 

public static void main(String[] args) throws FileNotFoundException, IOException {
        
        String path  = "d:/chars.txt";
        //saveFile( path );
        
        //String path = "e:/javaex623/BreakTest.java";
        
        readFile( path );
    }
    
    //解决FileInputStream类的对象在读取汉字并显示时乱码理解
    public static void readFile( String path ) throws FileNotFoundException , IOException {
        FileInputStream  fis = null;
        InputStreamReader  isr = null;
        
        try {
            fis = new FileInputStream( path );
            isr = new InputStreamReader( fis , "utf-8"); //将fis文件的字节输入流转换成字符输入流。
            
            int x = isr.read(); //fis.read();
            while( x != -1 ){
                System.out.print( (char) x  );
                
                x = isr.read(); //fis.read();
            }
        
            System.out.println("\n读取完毕。");
            
        } finally {
            if( isr != null )
                isr.close();
            
            if( fis != null )
                fis.close();
        }
    }
    
    //解决用FileOutputStream对汉字存盘时失真现象
    public static void saveFile( String path ) throws FileNotFoundException , IOException {
        FileOutputStream  fos = null;
        char[] chs = {'欢','迎','你','们','来','到','尚','学','堂','学','习','!'};
        
        OutputStreamWriter  osw = null;
        
        try { 
            fos = new FileOutputStream( path );
            
            //将给定的字符转换成字节,再以字节流方式输出到fos文件的字节输出流中。即:最后以字节方式存盘。
            osw = new OutputStreamWriter( fos , "utf-8" );  //在存盘时,以"UTF-8"字符编码集来管理文字
            
            for(int i = 0; i < chs.length;  i++ ){
                osw.write( chs[i] );  //fos.write( chs[i] );
            }
            
            osw.flush(); //fos.flush();
            
            System.out.println("\n已将 " + Arrays.toString( chs ) + " 存盘到 " + path + "文件中了。");
        }finally {
            if( osw != null )
                osw.close();
            if( fos != null ){
                fos.close();
            }
        }

    }
View Code

 

package com.bjsxt.d821;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class ReadHomePageTest {
    public static void main(String[] args) {
        URL  obj = null;
        InputStreamReader  isr = null;
        
        try {
            obj = new URL( "http://www.baidu.com" );
            
            isr = new InputStreamReader( obj.openStream() , "utf-8");
            
            int x = isr.read();
            while( x != -1 ){
                System.out.print( (char)x  );
                x = isr.read();
            }
            
            System.out.println("\n读取 百度主页内容完毕。 ");
            
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                isr.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
}
View Code

 

posted on 2016-10-01 19:59  ZQ醒梦千年  阅读(306)  评论(0编辑  收藏  举报