package com.ywx.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; /** * 字节和字符的转换操作 * @author Vashon * date:20150403 */ public class OutputStreamWriteDemo { public static void main(String args[]) throws Exception{ File f=new File("d:"+File.separator+"testdemo.txt"); stream1(f); stream2(f); } //写入:字符-->字节 public static void stream1(File file) throws Exception{ Writer out=null; //将字符流转换成字节流(写入的时候) out=new OutputStreamWriter(new FileOutputStream(file)); out.write("这是一个美好的世界...hello world!"); out.close(); }//读取:字节-->字符 public static void stream2(File file) throws Exception{ Reader reader=null; reader=new InputStreamReader(new FileInputStream(file)); char c[]=new char[1024]; int len=reader.read(c); reader.close(); System.out.println(new String(c,0,len)); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。