A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 开弓没有回头箭 于 2015-5-31 10:48 编辑

在同一个类中,同时创建了多个字节输入流缓冲区对象与多个字节输入流缓冲对象,它们的缓冲区是共享的,用的都是一个缓冲区,用下面程序得到证明:
一个输入缓冲区对象对多个输出缓冲区对象:
  1. import java.io.*;
  2. class test1
  3. {
  4.         public static void main(String[] args)throws IOException
  5.         {
  6.                 copy();
  7.         }
  8.         public static void copy()throws IOException
  9.         {
  10.                 BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("in.txt"));//建立一个字节流输入缓冲区
  11.                 BufferedOutputStream bufos1 = new BufferedOutputStream(new FileOutputStream("ou1.txt"));//建立一个字节流输出缓冲区
  12.                 BufferedOutputStream bufos2 = new BufferedOutputStream(new FileOutputStream("out2.txt"));//建立第二个字节输出流缓冲区对象
  13.                 int by=0;
  14.                 int count=0;
  15.                 while((by=bufis.read())!=-1)//对于同一个输入流缓冲区对象,分别用两个输出流输入
  16.                 {
  17.                         if(count%2==0)
  18.                         {
  19.                                 bufos1.write(by);//输出缓冲区对象一输出一次
  20.                                 count++;
  21.                         }
  22.                         else
  23.                         {
  24.                                 bufos2.write(by);//输出缓冲区对象二输出一次
  25.                                 count++;
  26.                         }
  27.                 }
  28.                 bufos2.close();
  29.                 bufos1.close();
  30.                 bufis.close();
  31.         }
  32. }
复制代码

在上面程序中in.txt的类容为:123456789
所得结果,out1.txt的类容为:13579
out2.txt的类容为:2468
多个输入对一个输出缓冲区:
  1. import java.io.*;
  2. class test1
  3. {
  4.         public static void main(String[] args)throws IOException
  5.         {
  6.                 copy();
  7.         }
  8.         public static void copy()throws IOException
  9.         {
  10.                 BufferedInputStream bufis1 = new BufferedInputStream(new FileInputStream("in1.txt"));//建立一个字节流输入缓冲区
  11.                 BufferedInputStream bufis2 = new BufferedInputStream(new FileInputStream("in2.txt"));//建立第二个字节流输入缓冲区
  12.                 BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("out.txt"));//建立一个字节输出流缓冲区对象
  13.                 int by=0;
  14.                 int count=0;
  15.                 boolean []fileEnd={false,false};
  16.                 while(true)//对于两个输入流缓冲区对象用同一个输出流
  17.                 {
  18.                         if(count%2==0&&fileEnd[0]==false)
  19.                         {
  20.                                 if((by=bufis1.read())==-1)
  21.                                 {
  22.                                         fileEnd[0]=true;
  23.                                 }
  24.                                 count++;
  25.                         }
  26.                         else if(fileEnd[1]==false)
  27.                         {
  28.                                 if((by=bufis2.read())==-1)
  29.                                 {
  30.                                         fileEnd[1]=true;
  31.                                 }
  32.                                 count++;
  33.                         }
  34.                         if(fileEnd[0]==true&&fileEnd[1]==true)
  35.                         {
  36.                                 break;
  37.                         }
  38.                         if(by!=-1)
  39.                         {
  40.                                 bufos.write(by);
  41.                         }
  42.                 }
  43.                 bufos.close();
  44.                 bufis2.close();
  45.                 bufis1.close();
  46.         }
  47. }
复制代码

在上面程序中in1.txt的类容为:13579
所得结果,in2.txt的类容为:2468
out2.txt的类容为:123456789
由此可见在输入输出流缓冲区对象是共享同一个缓冲区,该缓冲区应该就是单例模式

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马