字节流转字符流

来源:互联网 发布:windows软件功能 编辑:程序博客网 时间:2024/04/29 22:12


package test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.Reader;import java.io.Writer;public class Test {        public static void main(String[] args) throws IOException {    outputStreamToWriter();    inputStreamToReader();    }        public static void outputStreamToWriter() throws IOException{    File f=new File("d:"+File.separator+"text.txt");    Writer writer=new OutputStreamWriter(new FileOutputStream(f));    writer.write("Hello World!");    writer.close();    }        /**输入: 字节流转字符流      * @throws IOException **/    public static void inputStreamToReader() throws IOException{    File f=new File("d:"+File.separator+"text.txt");    Reader 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));    }            /**     * 输出Hello World!     */}


0 0
原创粉丝点击