package com.io; import java.io.*; public class OutputStreamWriterDemo01 { public static void main(String args[]) throws Exception{ //所有异常抛出 //写入数据 File file=new File("d:"+File.separator+"test.txt"); Writer writer=null; //字符输出流 writer=new OutputStreamWriter(new FileOutputStream(file)); //字节流变为字符流 String str="hello world!!!!"; writer.write(str); //使用字符流输出 writer.close(); //读取文件 File f = new File("d:" + File.separator + "test.txt") ; Reader reader = null ; reader = new InputStreamReader(new FileInputStream(f)) ; // 将字节流变为字符流 char c[] = new char[1024] ; int len = reader.read(c) ; // 读取 reader.close() ; // 关闭 System.out.println(new String(c,0,len)) ; } }
java提供将字节流转化为字符流读写方式的OutputStreamWriter和InputStreamReader