黑马程序员——IO流

来源:互联网 发布:淘宝图片压缩工具 编辑:程序博客网 时间:2024/06/14 07:55
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

最近学习了Java的IO流部分,下面作一些简单的总结:

   首先,Java中IO流的作用是处理设备之间的数据传输。按照操作数据可以分为字符流和字节流,按照流向可以分为输入流和输出流。Java用来操作流的对象都封装在io包中,下面我引用了一张别人的框架图,清晰地罗列出了IO流的体系结构。

下面,对一些常用的流举例示范:

字符流

    //对FileReader和FileWriter的演示:    //将指定文件的内容复制到另一个文件中public static void copyFile1(){FileReader fr = null;FileWriter fw = null;try{fr = new FileReader("a.txt");//创建字符流的来源fw = new FileWriter("b.txt");//创建字符流的去向char[] arr = new char[1024];//定义一个字符数组作为容器,使复制更高效int len = 0;while((len=fr.read(arr))!=-1)fw.write(arr,0,len);}catch(IOException e){throw new RuntimeException("读写失败");}finally{if(fr!=null)try{ fr.close();//关闭输入流}catch(IOException e){throw new RuntimeException("关闭输入流失败");}if(fw!=null) try{fw.close();//关闭输出流}catch(IOException e){throw new RuntimeException("关闭输出流失败");}}}
以上示例是将一个文件的内容复制到另一个文件里,为了提高效率,我们定义了一个长度为1024的字符数组来存放字符,再将数组中的内容一次性写到指定文件,从而避免了一个字符一个字符地写入。

Java中使用装饰设计模式对读写功能进行了增强,即其内部提供一个数组来存放要写入的字符,对外仅提供实现方式,从而大大提高了读写效率,下面我们对上面的示例进行改进。

    /*BufferedReader和BufferedWriter演示对copyFile1()方法进行改良,使复制的效率更高效    */public static void copyFile2(){BufferedReader br = null;BufferedWriter bw = null;try{br = new BufferedReader(new FileReader("a.txt"));//对FileReader进行包装bw = new BufferedWriter(new FileWriter("b.txt"));//对FileWriter进行包装String line = null;while((line=br.readLine())!=null){//一次性读取一行字符bw.write(line);               //将整行字符写入bw.newLine();                 //换行}}catch(IOException e){throw new RuntimeException("读写失败");}finally{if(br!=null)try{ br.close();}catch(IOException e){throw new RuntimeException("关闭输入流失败");}if(bw!=null)try{bw.close();}catch(IOException e){throw new RuntimeException("关闭输出流失败");}}}

字节流

字符流只能操作文本文件,如果需要操作的是非文本文件,例如:音频、视频、图片等,就需要使用到字节流。

以下示例是复制一张图片,具体操作与上面的复制文本的操作相似,只是将字符流改为了字节流而已。

        /*对FileInputStream和FileOutputStream的演示复制图片a.jpg为b.jpg*/public static void copyPicture1(){FileInputStream fi = null;FileOutputStream fo = null;try{fi = new FileInputStream("a.jpg");//定义字节流来源fo = new FileOutputStream("b.jpg");//定义字节流去向byte[] arr = new byte[1024];    //定义字节数组,使复制更高效int len = 0;while((len=fi.read(arr))!=-1)//将字节读入数组中fo.write(arr,0,len);//将数组的内容写入fo}catch(IOException e){throw new RuntimeException("读写失败");}finally{if(fi!=null)try{ fi.close();//关闭字节输入流}catch(IOException e){throw new RuntimeException("关闭输入流失败");}if(fo!=null)try{fo.close();//关闭字节输出流}catch(IOException e){throw new RuntimeException("关闭输出流失败");}}}
与字符流相似,Java对字节流也进行了包装,使其更高效:

public static void copyPicture2(){BufferedInputStream bfi = null;BufferedOutputStream bfo = null;try{bfi = new BufferedInputStream(new FileInputStream("a.jpg"));bfo = new BufferedOutputStream(new FileOutputStream("b.jpg"));int len = 0;while((len=bfi.read())!=-1)//读取输入流bfo.write(len);//写入输出流}catch(IOException e){throw new RuntimeException("读写失败");}finally{if(bfi!=null)try{ bfi.close();}catch(IOException e){throw new RuntimeException("关闭输入流失败");}if(bfo!=null)try{bfo.close();}catch(IOException e){throw new RuntimeException("关闭输出流失败");}}}

打印流

该流提供了打印方法,可以将各种数据类型原样打印。打印流也分为字节打印流和字符打印流。

字节打印流     即PrintStream

构造函数可以接收的参数类型:

1.File对象。File

2.字符串路径。String

3.字节输出流。OutputStream

字符打印流     即PrintWriter

构造函数可以接收的参数类型:

1.File对象。File

2.字符串路径。String

3.字节输出流。OutputStream

4.字符输出流。Writer

因为字符打印流构造函数可以接收参数的类型更多,所以,字符打印流更加常用。




0 0