一、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);//将读取到的字符存到数组中。返回读取的字符数。
- 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 {
-
-
- 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();
- }
二、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();//将该流中的缓冲数据刷到目的地中去。
- 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;
- 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();
- }
流转换程序1:
流转换程序2:
- 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();
- }
- }