一、InputStreamReader类
InputStreamReader 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。
构造方法:
InputStreamReader isr = new InputStreamReader(InputStream in);//构造一个默认编码集的InputStreamReader类
InputStreamReader isr = new InputStreamReader(InputStream in,String charsetName);//构造一个指定编码集的InputStreamReader类。
参数 in对象通过 InputStream in = System.in;获得。//读取键盘上的数据。
或者 InputStream in = new FileInputStream(String fileName);//读取文件中的数据。可以看出FileInputStream 为InputStream的子类。
主要方法:int read();//读取单个字符。
int read(char []cbuf);//将读取到的字符存到数组中。返回读取的字符数。
[java] view plain copy print ?
- public static void transReadNoBuf() throws IOException {
-
-
-
-
-
- InputStream in = new FileInputStream("D:\\demo.txt");
-
- InputStreamReader isr = new InputStreamReader(in);
-
-
- 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 {
-
-
-
-
-
- InputStream in = new FileInputStream("D:\\demo.txt");
-
- InputStreamReader isr = new InputStreamReader(in);
-
- BufferedReader bufr = new BufferedReader(isr);
-
-
-
-
- String line = null;
- while((line = bufr.readLine())!=null){
- System.out.println(line);
- }
- isr.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();
}
二、OutputStreamWriter类
OutputStreamWriter 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。
构造方法:
OutputStreamWriter osw = new OutputStreamWriter(OutputStream out);//构造一个默认编码集的OutputStreamWriter类
OutputStreamWriter osw = new OutputStreamWriter(OutputStream out,String charsetName);//构造一个指定编码集的OutputStreamWriter类。
参数 out对象通过 InputStream out = System.out;获得。//打印到控制台上。
或者 InputStream out = new FileoutputStream(String fileName);//输出到文件中。可以看出FileoutputStream 为outputStream的子类。
主要方法:void write(int c);//将单个字符写入。
viod write(String str,int off,int len);//将字符串某部分写入。
void flush();//将该流中的缓冲数据刷到目的地中去。
[java] view plain copy print ?
- public static void transWriteNoBuf() throws IOException {
- OutputStream out = System.out;
-
- OutputStreamWriter osr = new OutputStreamWriter(out);
-
-
-
-
- String str = "你好吗?";
- osr.write(str);
- osr.flush();
- osr.close();
- }
- public static void transWriteByBuf() throws IOException {
-
- OutputStream out = new FileOutputStream("D:\\demo.txt");
- OutputStreamWriter osr = new OutputStreamWriter(out);
-
- BufferedWriter bufw = new BufferedWriter(osr);
-
-
-
- String str = "你好吗?\r\n我很好!";
- bufw.write(str);
- bufw.flush();
- bufw.close();
- }
public static void transWriteNoBuf() throws IOException {
OutputStream out = System.out;//打印到控制台
// OutputStream out = new FileOutputStream("D:\\demo.txt");//打印到文件
OutputStreamWriter osr = new OutputStreamWriter(out);//输出
// OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\\demo.txt"));//两句可以综合到一句。
// int ch = 97;//a
// int ch = 20320;//你
// osr.write(ch);
String str = "你好吗?";//你好吗?
osr.write(str);
osr.flush();
osr.close();
}
public static void transWriteByBuf() throws IOException {
// OutputStream out = System.out;//打印到控制台。
OutputStream out = new FileOutputStream("D:\\demo.txt");//打印到文件。
OutputStreamWriter osr = new OutputStreamWriter(out);//输出
// OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\\demo.txt"));//综合到一句。
BufferedWriter bufw = new BufferedWriter(osr);//缓冲
// int ch = 97;//a
// int ch = 20320;//你
// osr.write(ch);
String str = "你好吗?\r\n我很好!";//你好吗?
bufw.write(str);
bufw.flush();
bufw.close();
}
流转换程序1:
package IOtest;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class TransStreamtest {
/**
* 主要的类: in1, InputStream
* 创建对象 InputStream in = System.in;
* in2, InputStreamReader 没有readLine()方法
* 主要方法:
* read()读取单个字符,一个汉字也为一个字符。
* read(char[] cbuf)将字符读入数组。
* close().关闭此流和相关联资源。
* in3, BufferedReader 有read(),readLine()方法。
* out1, OutputStream
* 创建对象 OutputStream in = System.out;
* out2, OutputStreamWriter
* 主要方法:
* write(int c)//写入单个字符。
* write(char[] cbuf,int off,int len)//写入数组的某一部分
* write(String str,int off,int len)//写入字符串烦人某一部分。
* flush();//刷新该流中的缓冲。
* close();
* out3, BufferedWriteer 有Write(int ch),newLine()方法。
*
*
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// transReadByBuf();
// transReadNoBuf();
transWriteNoBuf();
// transWriteByBuf();
}
public static void transWriteNoBuf() throws IOException {
OutputStream out = System.out;//打印到控制台
// OutputStream out = new FileOutputStream("D:\\demo.txt");//打印到文件
OutputStreamWriter osr = new OutputStreamWriter(out);//输出
// OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\\demo.txt"));//两句可以综合到一句。
// int ch = 97;//a
// int ch = 20320;//你
// osr.write(ch);
String str = "你好吗?";//你好吗?
osr.write(str);
osr.flush();
osr.close();
}
public static void transWriteByBuf() throws IOException {
// OutputStream out = System.out;//打印到控制台。
OutputStream out = new FileOutputStream("D:\\demo.txt");//打印到文件。
OutputStreamWriter osr = new OutputStreamWriter(out);//输出
// OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\\demo.txt"));//综合到一句。
BufferedWriter bufw = new BufferedWriter(osr);//缓冲
// int ch = 97;//a
// int 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] view plain copy print ?
- 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 {
-
-
-
-
-
- public static void main(String[] args) throws IOException {
-
-
-
-
-
-
-
- }
-
-
- 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 = new OutputStreamWriter(new FileOutputStream("D:\\utf-8.txt"),"UTF-8");
- osw.write("你好吗");
- osw.close();
- }
-
- public static void writeText_2() throws IOException {
- FileWriter fw = new FileWriter("D:\\gbk1.txt");
- fw.write("你好啊");
- fw.close();
- }
-
- public static void writeText_1() throws IOException {
- OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\gbk.txt"),"GBK");
-
-
-
-
-
- osw.write("你好吗");
- osw.close();
- }
- }