转换流 字节到字符的转换(解码,读取) 字符到字节(编码,写入)
1 package Iotext; 2 3 4 /** 5 * 转换流:字节转换为字符 6 *1.输出流:OutputstreamWriter 编码 7 *2.输入流 :InputStreamReader 解码 8 */ 9 10 11 import java.io.BufferedInputStream; 12 import java.io.BufferedOutputStream; 13 import java.io.BufferedReader; 14 import java.io.BufferedWriter; 15 import java.io.File; 16 import java.io.FileInputStream; 17 import java.io.FileOutputStream; 18 import java.io.IOException; 19 import java.io.InputStreamReader; 20 import java.io.OutputStreamWriter; 21 22 23 24 public class ConverDemo02 { 25 26 /** 27 * @param args 28 * @throws IOException 29 */ 30 public static void main(String[] args) throws IOException { 31 //指定解码字符集 32 BufferedReader br =new BufferedReader( 33 //字节流个字符流不能直接操作,加一个InputStreamreader转换流 34 new InputStreamReader( 35 new 36 BufferedInputStream( 37 38 new FileInputStream( 39 new File("E:/xp/test/Demo03.java"))),"UTF-8") 40 ); 41 //写出文件 编码 42 BufferedWriter bw =new BufferedWriter( 43 new OutputStreamWriter( 44 new BufferedOutputStream( 45 new FileOutputStream(new 46 File("E:/xp/test/encode.java" 47 ))))); 48 49 String info =null; 50 while(null!=(info=br.readLine())){ 51 //System.out.println(info); 52 bw.write(info); 53 bw.newLine(); 54 } 55 bw.flush(); 56 bw.close(); 57 br.close(); 58 } 59 60 }