Reader是带编码转换器的InputStream,它把byte转换为char,而Writer就是带编码转换器的OutputStream,它把char转换为byte并输出。from
FileWriter
try(Writer writer=new FileWriter("filename",charset);){
writer.write(123);//写入char(123)
writer.write("hello");//写入String
writer.write("hello".toCharArray());//写入char[]
}
其它类型的Writer
CharArrayWriter,StringWriter
try(Writer writer =new CharArrayWriter();Writer writer2=new StringWriter();){
writer.write(123);
writer.write("22d");
writer2.write(2);
writer2.write("hello");
System.out.println(writer2.toString());
}
OutputStramWriter
try(Writer writer=new OutputWriter(new FileOutputStream("filename"),"utf-8"))