Java IO流系列(四)—— 从字节流及其缓冲区到转换流

来源:互联网 发布:图片写字软件 编辑:程序博客网 时间:2024/04/29 17:55

前言:字节流和前面的字符流在用法上大同小异,故而简述带过,不再详述。主要是搞清楚两者的差异,知道什么时候用字节流,比如说我们的图片,System.in, System.out都是字节流。本文将以复制图片的小例子简述字节流及其缓冲区,最后引出转换流与前面的字符流做统一。
原文出处:http://blog.csdn.net/u014158743/article/details/52695142

使用字节流复制图片

图片是以字节的形式存储的,故而只能用字节流不能用字符流。

public static void main(String[] args) {    //使用字节流复制图片    FileInputStream  fis = null;    FileOutputStream fos = null;    try    {        fis = new FileInputStream("s.jpg");        fos = new FileOutputStream("s_copy.jpg");        byte[] arr = new byte[1024];        int len =0;        while((len = fis.read(arr))!=-1)        {            fos.write(arr,0,len);        }    }    catch (IOException e)    {        throw new RuntimeException("图片复制失败");    }    finally    {        if(fis!=null)            try            {                fis.close();            }            catch (IOException e)            {                throw new RuntimeException("文件读取流关闭失败");            }        if(fos!=null)            try            {                fos.close();            }            catch (IOException e)            {                throw new RuntimeException("文件写入流关闭失败");            }    }}

使用字节流缓冲区复制图片

套上缓冲区减少磁盘读写次数,可以增加传输速率,提高性能。

public static void main(String[] args) {    //使用字节流的缓冲区复制图片    BufferedInputStream bis = null;    BufferedOutputStream bos = null;    try    {        bis = new BufferedInputStream(new FileInputStream("s.jpg"));        bos = new BufferedOutputStream(new FileOutputStream("s_copy_2.jpg"));        byte[] arr = new byte[1024];        int len = 0;        while((len = bis.read(arr))!=-1)        {            bos.write(arr,0,len);        }    }    catch (IOException e)    {        e.printStackTrace();    }    finally    {        if(bis!=null)            try            {                bis.close();            }            catch (IOException e)            {                e.printStackTrace();            }        if(bos!=null)            try            {                bos.close();            }            catch (IOException e)            {                e.printStackTrace();            }    }}

转换流

/*使用 System.in实现键盘循环读取数据,实现的代码比较麻烦可以用BufferedReader的readLine()方法的实现,简化书写使用BufferedReader的readLine()方法的实现键盘录入数据键盘输入流:System.in---字节输入流BufferedReader:字符输入流把字节输入流转成字符输入流:转换流:InputStreamReader:把字节流转成字符流OutputStreamWriter:*/import java.io.*;class Demo{    public static void main(String[] args)throws IOException     {        //标准的输入流        InputStream in = System.in;        //把字节输入流转成字符输入流        InputStreamReader isr = new InputStreamReader(in);        //为了提高效率,把字符输入流传递给BufferedReader的构造方法        BufferedReader br = new BufferedReader(isr);       //标准的输出流        PrintStream  out = System.out;        //把字节输出流转成字符输出流        OutputStreamWriter osw = new OutputStreamWriter(out);        //为了提高效率,把字符输出流传递给BufferedWriter的构造方法        BufferedWriter bw = new BufferedWriter(osw);        String line = null;        while((line = br.readLine())!=null)        {            if("over".equals(line))                break;            //out.println(line);            bw.write(line);            bw.newLine();//向控制台写入换行            bw.flush();        }        br.close();    }}

指定输出编码格式

class Demo{    public static void main(String[] args) throws IOException    {        OutputStreamWriter  osw = new OutputStreamWriter(new FileOutputStream("utf.txt"),"utf-8");        osw.write("你好");        osw.close();    }}

System.in和System.out

import java.io.*;class  Demo{    public static void main(String[] args) throws IOException    {        //System.setIn(new FileInputStream("temp.txt"));//改变标准的输入        BufferedReader  br = new BufferedReader(new InputStreamReader(System.in));        System.setOut(new PrintStream("temp2.txt"));//改变标准的输出        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));        String line = null;        while((line = br.readLine())!=null)        {            if("over".equals(line))                break;            bw.write(line);            bw.newLine();            bw.flush();        }        br.close();        bw.close();    }}

Java IO流系列(三)—— 字符流的缓冲区
http://blog.csdn.net/u014158743/article/details/52675871

Java IO流系列(一)—— 启动篇
http://blog.csdn.net/u014158743/article/details/52664986

1 0