by Dinesh Thakur

The package provides a set of abstract classes that define and partially implement Filter streams. A Filter stream filters data as it is being read from or written to the stream. The two filter streams for reading and writing data are Filter input Stream and Filter Output Stream ,respectively.


A filter stream is constructed on another stream. That is, every filtered stream must be attaché to another stream. This can be achieved by passing an instance of Input Stream or Output Stream to a constructor. This implies that filter streams are chained. Multiple filters can be added by chaining them to a stream of bytes. The read method in a readable filter stream reads input from the underlying stream, filters it and passes on the filtered data to the caller. The write method in a writable filter stream filters the data and then writes it to the underlying stream. The filtering done by the streams depends on the stream. Some streams buffer the data, some count data as they go by and others convert data from the original form to a different one. Most filter streams provided by the package are sub-classes of Filter input Stream and Filter Output Stream, and are listed below:

   • Data input Stream and Data Output Stream

   • Buffered input Stream and Buffered Output Stream

   • Line Number input Stream

   • Push back input Stream

   • Print Stream

The package contains only one sub-class of Filter Reader known as Push back Reader.

To create filter input or output stream, we should attach the filter stream to another input or output stream. For example, we can attach a filter stream to the standard input stream in the following manner:

 BufferedReader d = new BufferedReader(new InputStreamReader(System. in));

                 String input;

                 While((input = d.readLine()) != null)




The above code is useful for reading the data from the console. Here, Input Stream Reader is attached to Buffered Reader which acts as the filter stream.

Data input Stream and Data Output Stream

Data input Stream (Data Output Stream) is a filtered input (output) stream and hence must be attached to some other input (output) stream. Data input Stream handles reading in data as well as lines of text. Program stores the data which are read from the console in tabular format using Data Output Stream. After that, the program reads the data using the Data input Stream.

Program Using Data Output Stream and Data input Stream.


class Sample Stream


      public static void main(String a[ ]) throws IOException


            DataOutputStream out = new DataOutputStream(new

                                                       File Output Stream("file.txt"));

            InputStreamReader isr = new Input Stream Reader(System. in);

            Buffered Reader is = new BufferedReader(isr);

            char sep = System.get Property("file.separator").charAt(0);

            for (int i = 0; i < 5; i ++)


                  System.out.print("Read the item name:");

                  String item=br.readLine();



                  System.out. print("Read the quantity:");

                  int qun=integer.Parselnt(br.readLine());





            DatalnputStream in = new DatalnputStream(new




                  char chr;

                  while (in.avaiiable()>0)


                       StringBuffer item1 = new StringBuffer(20);

                       while ((chr = in.readChar())!= sep)


                            item1.append( chr);



                        System.out.print(in.readChar() );

                        System.out.print( Int());                      

                        System.out.print(in.readChar() );



           catch (EOFException e)






Running Program  gives the following output:

              Read the item name: Mouse

               Read the quantity: 4

               Read the item name: Keyboard

               Read the quantity: 3

               Read the item name: Monitor

               Read the quantity: 2

               Read the item name: Modem

               Read the quantity: 4

               Read the item name: Hub

               Read the quantity: 4

             Mouse 4

              Keyboard 3

             Monitor 2

              Modem 4

              Hub 4

The above example stores the items required and the quantity of the item. Although the data are read or written in binary form, they appear on the console as characters .Data Output Stream is attached to a File Output Stream that is set up to write to a file namedfile.txt. Data Output Stream writes the data in the file.txt file by using specialized write XXX()methods. Here, XXX represents a Java data type. Next, Sample Stream opens a Data input Stream .on the file just written to read the data from it by using Data input Stream's specialized read XXX()methods. In general, we use null or -1 to indicate the end of the file. Some methods such as read String()and read Char() in the Data input Stream do not identify null or-1 as an end of file marker. Thus, programmer must be careful while writing loops using Data Input stream for reading characters from a file. The available() method of Data input Stream, which returns the number of bytes remaining, can be used to check whether there are any data remaining to read.

Push back input Stream

Pushback is used on input streams to allow a byte to be read from and returned to the stream. Like all filter streams, Push back input stream is also attached to another stream. It is typically used for some specific implementation, for example, to read the first byte of the data from a stream forth purpose of testing the data. For example, it is mostly required in the case of designing parsers, while reading data, to have glance on the next set of input data. Data must be read and if not found appropriate, it should be placed back into the input stream, which may be read again and processed normally. A Push back input Stream object can be created as follows:

Push back input Stream imp = new Push back input Stream(new Input Stream);

In order to satisfy the requirement of pushing back a byte (or bytes) of an array into the input stream, this stream has the special method called the unread () method.

                        void unread( int ch );

                        void unread( bute buf[ ] );

                        void unread( byte buf[ ], int offset, int numchars );

the first form pushes back the lowest-order byte of ch, which is returned as the next byte when another byte is read. The second and the third forms of the method return the byte in a buffer with the difference that the third method pushes back the number of characters (numchars) starting from the offset. Push back Reader also does the same thing on the character stream.