InputStreamReader 和 OutputStreamWriter类用法简介,及演示。(三)
nt ch = 20320;//你
// osr.write(ch);
String str = "你好吗?\r\n我很好!";//你好吗?
bufw.write(str);
bufw.flush();
bufw.close();
}
public static void transReadNoBuf() throws IOException {
/**
* 没有缓冲区,只能使用read()方法。
*/
//读取字节流
// InputStream in = System.in;//读取键盘的输入。
InputStream in = new FileInputStream("D:\\demo.txt");//读取文件的数据。
//将字节流向字符流的转换。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节.
InputStreamReader isr = new InputStreamReader(in);//读取
// InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\demo.txt"));//综合到一句。
char []cha = new char[1024];
int len = isr.read(cha);
System.out.println(new String(cha,0,len));
isr.close();
}
public static void transReadByBuf() throws IOException {
/**
* 使用缓冲区 可以使用缓冲区对象的 read() 和 readLine()方法。
*/
//读取字节流
// InputStream in = System.in;//读取键盘上的数据
InputStream in = new FileInputStream("D:\\demo.txt");//读取文件上的数据。
//将字节流向字符流的转换。
InputStreamReader isr = new InputStreamReader(in);//读取
//创建字符流缓冲区
BufferedReader bufr = new BufferedReader(isr);//缓冲
// BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\demo.txt")));可以综合到一句。
/* int ch =0;
ch = bufr.read();
System.out.println((char)ch);*/
String line = null;
while((line = bufr.readLine())!=null){
System.out.println(line);
}
isr.close();
}
}
流转换程序2:
[java]
package readKey;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class TransStreamDemo3 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// writeText_1();
// writeText_2();
// writeText_3();
// ReadTest_1();
// ReadTest_2();
// ReadTest_3();
}
public static void ReadTest_3() throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\utf-8.txt"),"UTF-8");
char []ch = new char[20];
int len = isr.read(ch);
System.out.println(new String(ch,0,len) );
isr.close();
}
public static void ReadTest_2() throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\utf-8.txt"),"GBK");
char []ch = new char[20];
int len = isr.read(ch);
System.out.println(new String(ch,0,len) );
isr.close();
}
public static void ReadTest_1() throws IOException {
FileReader fr = new FileReader("D:\\demo.txt");
char []ch = new char[20];
int len = fr.read(ch);
System.out.println(new String(ch,0,len) );
fr.close();
}
public static void writeText_3() throws IOException {
OutputStreamWriter osw