InputStreamReader与OutputStreamWriter 也是处理流
package TransformStream;import org.junit.Test;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;public class InputStreamReaderTest { @Test public void test1() throws IOException { FileInputStream fis = new FileInputStream("hello2.txt");// 使用默认的字符集 将 字节流的数据转换(在IDEA中为默认UTF-8) InputStreamReader isr = new InputStreamReader(fis);// 指定字符集 将 字节流的数据转换// InputStreamReader isr = new InputStreamReader(fis,"gbk"); char[] buffer = new char[5]; int len ; while((len = isr.read(buffer))!=-1){ String str= new String(buffer); System.out.print(buffer); }// 转换流也是处理流,所以只需要关闭外层 isr.close(); }}
OutputStreamWriter
// OutputStreamWriter @Test public void test2() throws IOException { FileReader fr = new FileReader("hello2.txt"); FileOutputStream fos = new FileOutputStream("hello3.txt"); OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk"); char[] buffer = new char[5]; int len; while((len = fr.read(buffer))!=-1){ osw.write(buffer,0,len); } fr.close(); osw.close(); }