OutputStreamWriter

2/10/2017来源:ASP.NET技巧人气:1605

package inputDemo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; /** * OutputStreamWriter类用来按照指定的字符集编码将字符流转换成字节流; * * @author king_wang * */ public class OutputStreamWriterDemo { public static void main(String[] args) throws IOException{ OutputStreamWriter osw=new OutputStreamWriter( new FileOutputStream("E:\\file.txt"),"utf-8"); osw.append("我爱北京天安门,天安门上太阳升。"); osw.write("我家住在黄土高坡,大风从哪里刮过"); osw.close(); InputStreamReader isr=new InputStreamReader( new FileInputStream("E:\\file.txt"),"utf-8"); int c=-1; while((c=isr.read())!=-1){ System.out.PRint((char)c); } isr.close(); } }

运行结果: 我爱北京天安门,天安门上太阳升。我家住在黄土高坡,大风从哪里刮过;