
![]() |
![]() |
public class SubTransStreamDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub /** * 转换流的子类,专门用于操作文本文件的流对象 */ writeText(); readText(); } private static void readText() throws IOException { // TODO Auto-generated method stub FileReader fr = new FileReader("H:\\workspace\\Testfile\\1.txt"); //等效于 // FileInputStream fis = new FileInputStream("H:\\workspace\\Testfile\\1.txt"); // InputStreamReader isr = new InputStreamReader(fis); int ch = 0; while((ch = fr.read()) != -1){ System.out.println((char)ch); } fr.close(); } private static void writeText() throws IOException { // TODO Auto-generated method stub //创建一个用于操作文件的字符输出流对象 FileWriter fw= new FileWriter("H:\\workspace\\Testfile\\1.txt");//默认码表GBK //上面一句等效于下面两行 // FileOutputStream fos = new FileOutputStream("H:\\workspace\\Testfile\\1.txt"); // OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk"); fw.write("你好"); fw.close(); } }