我已经得到inputstream流,需要把它输出到控制台,同时还要存在一个文件中,如何实现?

解决方案 »

  1.   

    byte[] b;
    InputStream is;
    is.read(b,0,b.length);
    System.out.write(b,0,length);
    文件一样操作!
      

  2.   

    多线程我尝试了,inputstream的内容被分割,存入文件的一部分,显示在控制台的一部分。
    这个问题的实质就是如何多次使用一个inputstream?
      

  3.   

    zhutouzip的意思是用操作数组代替inputstream?
      

  4.   

    不是代替,而是将inputstream中的字节读入一个缓存数组,然后将这个数组中的内容读出!
      

  5.   

    inputstream的内容读出以后还能对它进行操作吗?
      

  6.   

    如何获得inputstream的字节数?
      

  7.   

    byte[] buff = new byte[1024];
    InputStream is;
    int c;
    c = is.read(buff, 0, 1024);
    System.out.println(new String(byte));
    PrintWriter out = new PrintWriter( new OutputStreamWriter(new FileOutputStream("aa.txt")));
    out.print(new String(byte));
      

  8.   

    做成一个Chain,参照那些拦截器,过滤器的实现模式
      

  9.   

    示例代码,楼主参考下   
     try
        {
    //File fl = new File("c:\\cc\\c1.txt");
          FileReader input=new FileReader("AlphaComposite.java");
          FileWriter output=new FileWriter("temp2.txt");
          BufferedReader br=new BufferedReader(input);
          BufferedWriter bw=new BufferedWriter(output);
            String s=br.readLine();
          while(s!=null)
          {
            bw.write(s);
            bw.newLine();
            System.out.println(s);
            s=br.readLine();
          }
          br.close();
          bw.close();    }catch(IOException e){System.out.println(e);}
      

  10.   

    import java.io.* ;  public class   TeeOutputStream
             extends OutputStream
      {
          OutputStream tee = null, out = null;
      
      
         public TeeOutputStream(OutputStream chainedStream, 
                                 OutputStream teeStream)
          {
               out = chainedStream;
      
               if (teeStream == null)
                   tee = System.out;
               else
                   tee = teeStream;
          }
          
      
      /**
      * Implementation for parent's abstract write method.  
      * This writes out the passed in character to the both,
      * the chained stream and "tee" stream.
      */
           
          public void write(int c) throws IOException
          {
               out.write(c);
      
               tee.write(c);
               tee.flush();
          }
      
      
          /**
           * Closes both, chained and tee, streams.
           */
          public void close() throws IOException
          {
               flush();
      
               out.close();
               tee.close();
          }
      
      
       /**
       * Flushes chained stream; the tee stream is flushed 
       * each time a character is written to it.
       */
          public void flush() throws IOException
          {
               out.flush();
          }
      
      
      
       /** Test driver */
       public static void main(
         String args[]) throws Exception
          {
              FileOutputStream fos =
                    new FileOutputStream("test.out");
              TeeOutputStream  tos =  
                    new TeeOutputStream(fos, System.out);
              PrintWriter      pw  =  
              new PrintWriter(new OutputStreamWriter(tos));
              
               pw.println("Testing line 1");
               pw.println("Testing line 2");
               pw.close();
          }
      }
      这是我在网上找的一个例子,它没有缓冲区大小的限制,可以少加改造,就可以满足需要;现在还有个问题和大家讨论,就是这个Tee类输出的是两个outputstream,如何把其中一个outputstream变为原来的inputstream传给其它得类进行处理?