IO最基本操作:字节流及转换流

来源:互联网 发布:10月经济数据公布 编辑:程序博客网 时间:2024/06/05 16:28

字符流主要是操作纯文本文件,而字节流可以操作图片,音频,视频等,转换流可以实现字节流到字符流的转换。

    //(字节流)该方法可以放到main函数里直接调用    public static void method5() throws IOException    {        FileInputStream fis = new FileInputStream("E:\\IDEA_workspace\\img\\1.PNG");        FileOutputStream fos = new FileOutputStream("1.PNG");        byte[] b = new byte[1024];        int len;        while((len = fis.read(b))!=-1)        {            fos.write(b,0,len);        }        fis.close();        fos.close();    }
    //转换流,InputStreamReader和OutputStreamWriter    public static void method7() throws IOException//读写转换流    {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));        String line;        while((line = br.readLine())!=null)        {            if("over".equals(line))                break;            bw.write(line);            bw.newLine();            bw.flush();        }        br.close();        bw.close();    }
阅读全文
0 0
原创粉丝点击