黑马程序员技术交流社区

标题: IO字符流 [打印本页]

作者: 张敬培    时间: 2013-1-23 01:56
标题: IO字符流
  1. /*
  2. * 对一个照片进行复制
  3. */
  4. public class BufferedReaderDemo {
  5.         public static void main(String[] args) throws IOException {

  6.                 //创建输入流对象
  7.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
  8.                                 "F:\\下载\\google\\645396a3fb42d527bcae075bf3ebad71.jpg"));
  9.                 //创建输出流对象
  10.                 BufferedOutputStream bos = new BufferedOutputStream(
  11.                                 new FileOutputStream("D:\\bos.jpg"));
  12.                 //创建输出流对象
  13.                 PrintStream ps = new PrintStream("D:\\ps.jpg");

  14.                 //读取
  15.                 byte[] b = new byte[1024];
  16.                 int len = 0;

  17.                 while ((len = bis.read(b)) != -1) {
  18.                         bos.write(b, 0, len);

  19. //                        ps.write(b);
  20.                 }
  21.                 //释放资源
  22.                 bis.close();
  23.                 bos.close();
  24.                 ps.close();

  25.         }

  26. }
复制代码
在写入的时候,bos.write(b, 0, len);和pw.write(b);同时运行,那么图片都能显示出来。
如果注释掉pw.write(b);ps.jpg无法打开,而bos.jpg打开没有问题。
如果注释bos.write(b, 0, len);则bos.jpg无法打开,而ps.jpg能打开
这是为什么呢?

作者: ︶ㄣ布丁    时间: 2013-1-23 03:31
<一>:流:在程序中所有的数据都是以流的形式传输或者保存的,程序需要数据的时候就用到输入流来读取数据,当程序需要将数据保存起来的时候需要输出流来完成。程序的输入输出都是以流的形式保存的,流中保存的实际全是字节文件。
<二>字节流与字符流:在Java.io包中操作文件内容的主要有两大类: 字节流 与字符流,这两大类都有输入与输出操作。字节流输入操作用 InputStream  输出用 OutputStream。字符流输入输出操作分别是Reader与Writer。
<三>IO操作流程(以文件操作为例):1 以File类打开一个文件 ;2 通过字节流或者字符流的子类指定输出的位置;3 进行读/写操作;4关闭输入输出流。
<四> 字节流:
                输出流 OutputStream :
package com.iuumobile.test;
public class ByteDemo1 {
    public static void main(String[] args) {
        //1 利用File类打开一个文件
        File file=new File("c://haoran.txt");
        //2 利用输出流子类指定输出位置
        OutputStream output=null;
        try {
           output=new FileOutputStream(file,true);//添加boolean属性可以控制它能否覆盖原来内容,true为不覆盖
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //3 输出操作
        String str=new String("aaaaaaa");
        byte [] bytes =str.getBytes();
        try {
            output.write(bytes);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //4关闭输出流
        try {
            output.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
作者: 唐晓    时间: 2013-1-23 09:20
BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream("D:\\bos.jpg"));
PrintStream ps = new PrintStream("D:\\ps.jpg");
这是两个输出流对象
while ((len = bis.read(b)) != -1) {
             bos.write(b, 0, len);
                    ps.write(b);

   }
bos和ps是两个输出流,你注释掉随便哪一个,哪一个的数据就没有写入,数据就读不出来
所以如果注释掉ps.write(b);ps.jpg无法打开,而bos.jpg打开没有问题。
如果注释bos.write(b, 0, len);则bos.jpg无法打开,而ps.jpg能打开
如果你两个都注释掉,数据根本就没写入,一个都读不出来


作者: 黄锦成    时间: 2013-1-23 10:19
你现在是产生两张拷贝,bis对应bos.jpg;ps对应ps.jpg。被注释的那个,对应的图片无数据,当然打不开




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2