您的位置:首页 > 编程语言 > Java开发

Java基础知识强化之IO流笔记33:转换流之InputStreamReader的使用

2015-10-09 12:37 671 查看
1. InputStreamReader的使用

InputStreamReader(InputStream is):用默认的编码读取数据

InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据

2. 代码示例:

(1) InputStreamReader读方法下面两种经常用到两种方法:(继承自父类Reader)

 public int read()


 public int read(char[] cbuf)


(2)

 package cn.itcast_02;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/*
* InputStreamReader(InputStream is):用默认的编码读取数据
* InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
*/
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
// 创建对象
// InputStreamReader isr = new InputStreamReader(new FileInputStream(
// "osw.txt"));

// InputStreamReader isr = new InputStreamReader(new FileInputStream(
// "osw.txt"), "GBK");

InputStreamReader isr = new InputStreamReader(new FileInputStream(
"osw.txt"), "UTF-8");

// 读取数据
// 一次读取一个字符
int ch = 0;
while ((ch = isr.read()) != -1) {
System.out.print((char) ch);
}

// 释放资源
isr.close();
}
}


运行结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: