1. 程式人生 > >java.se.io.8.轉換流

java.se.io.8.轉換流

package com.knock.io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.junit.Test;

/**
 * @date 160802pm
 * 本類為轉換流,可以實現位元組字元之間的轉換,可以導致或者解決亂碼的問題,InputStreamReader;OutputStreamWriter;
 * 分別繼承自Reader和Writer
 * */
public class KiConver {
	@Test
	public void test(){
		File src = new File("D:/test/a.txt");
		BufferedReader br = null;
		try {
			//mark,把位元組流轉換成utf8的字元流,但是使用了utf8來開啟gbk檔案,造成了亂碼
			br = new BufferedReader(new InputStreamReader(new FileInputStream(src),"utf-8"));
			String info = null;
			while(null!=(info=br.readLine())){
				System.out.println(info);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(null!=br){
					br.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}