定义Java类实现字节流转字符流

package com.buaa.comparable;

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.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

public class TestRead {

/*static void parse(char[] c,String str){
str = str.copyValueOf(c);
try {
str = new String(str.getBytes("gbk"),"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}*/
public static void main(String[] args) {
testReader();

}
static void testReader(){
File f = new File("d:/a.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
int temp = 0;
while((temp=isr.read())!=-1){
char c = (char) temp;

System.out.print(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (fis!=null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

posted @ 2017-12-16 14:13  高登军  阅读(1356)  评论(0编辑  收藏  举报