java.io.InputStreamReader extends Reader 是字节流向字符的桥梁 解码作用 看不懂的转为能看懂的
继承自父类的成员方法:
int read()读取单个字符并返回
int read(char[] cbuf)
void close();
构造方法:
InputStreamReader(InputStream in) 创建一个使用默认字符集的InputStreamReader
inputStreamReader(InputStream in , String charsetName) 使用指定字符集的
参数:
InputStream in字节输入流,用来读取文件中保存的字节
String charsetName编码表名称 ,注意是String格式
使用步骤:
1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
2.使用InputStreaamReader对象中的方法read读取文件
3.释放资源
注意:构造方法中指定的编码表名称要和文件的编码相同,否则会发生乱码
package demo01;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/*
java.io.InputStreamReader extends Reader 是字节流向字符的桥梁 解码作用 看不懂的转为能看懂的
继承自父类的成员方法:
int read()读取单个字符并返回
int read(char[] cbuf)
void close();
构造方法:
InputStreamReader(InputStream in) 创建一个使用默认字符集的InputStreamReader
inputStreamReader(InputStream in , String charsetName) 使用指定字符集的
参数:
InputStream in字节输入流,用来读取文件中保存的字节
String charsetName编码表名称 ,注意是String格式
使用步骤:
1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
2.使用InputStreaamReader对象中的方法read读取文件
3.释放资源
注意:构造方法中指定的编码表名称要和文件的编码相同,否则会发生乱码
*/
public class Demo03InputStreamReader {
public static void main(String[] args) throws IOException {
read_utf_8();
}
/*
使用InputStreamReader读取UTF-8格式的文件
*/
private static void read_utf_8() throws IOException {
// 1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\IdeaProjects\\新建文件夹\\QQ.txt"),"gbk");//文件的编码格式与编码表格式要一致
//2.使用InputStreaamReader对象中的方法read读取文件
int len = 0;
while((len=isr.read())!=-1){
System.out.println((char)len);
}
isr.close();
}
}