Give Line Numbers LineNumberInputStream

Give Line Numbers LineNumberInputStream

Summary: By the end of this tutorial "Give Line Numbers LineNumberInputStream", you will come to know how to give line numbers using Java I/O streams.

LineNumberInputStream is a subclass of FilterInputStream stream. We know each filter stream adds some extra functionality. The LineNumberInputStream does the extra work of adding line numbers that do not exist in the source.

A program on give Line Numbers using byte stream LineNumberInputStream
public class LISDemo
  public static void main(String args[]) throws IOException
  {                                                                                   // reading-side
    String str1 = "The\nonly\ntrue\nwisdom\nis\nin\nknowing\nyou\nknow\nnothing\n";
    StringBufferInputStream sbstream = new StringBufferInputStream(str1);             // low-level
    LineNumberInputStream listream = new LineNumberInputStream(sbstream);             // high-level
    DataInputStream distrea m = new DataInputStream(listream);                        // high-level
					                                              // writing-side
    FileOutputStream fostream = new FileOutputStream("Quotes.txt");                   // low-level
    BufferedOutputStream bostream = new BufferedOutputStream(fostream);               // high-level
    PrintStream pstream = new PrintStream(bostream);                                  // high-level
    String str; 
    while((str = distream.readLine()) != null)
      String s1 = listream.getLineNumber() + ".  " + str;
    pstream.close();  bostream.close(); fostream.close();
    distream.close(); listream.close(); sbstream.close();

Give Line Numbers LineNumberInputStream Output screen of of Give Line Numbers LineNumberInputStream

StringBufferInputStream sbstream = new StringBufferInputStream(str1); LineNumberInputStream listream = new LineNumberInputStream(sbstream);
DataInputStream distream = new DataInputStream(listream);

The above three lines are an example of I/O streams chaining. The three statements can be replaced with anonymous objects to save memory.

DataInputStream distream = new DataInputStream(new LineNumberInputStream(new StringBufferInputStream(str1)));

Similarly we can do with writing side output streams also.


getLineNumber() method of LineNumberInputStream returns numbers that increments automatically. readLine() method of DataInputStream reads each line from string str1, delimited by \n, and getLineNumber() adds the line number.

When the above program is compiled, the compiler raises a warning message (see the above screen shot) as readLine() method of DataInputStream is deprecated.

A similar class on character streams is LineNumberReader which gives same functionality of adding line numbers.

5 thoughts on “Give Line Numbers LineNumberInputStream”

  1. Neeravi Pandian

    give me a idea to read the contents from text file and split it into equally three parts, and i want to write the splitted parts to three text files. I have splitted that and store the string[] array using split(), But i don’t have any idea to store the contents to three files. Please help me sir……….

    1. Check the code which I have got readily. If not suitable for your need, write me.

      // to copy first 3/4th part of source file to one destination file and the last 1/4th to another destination file.

      public class TwoFiles
      public static void main(String args[]) throws IOException
      FileInputStream fis = new FileInputStream(args[0]); // source file

      FileOutputStream fos1 = new FileOutputStream(args[1]); // 1st destination file

      FileOutputStream fos2 = new FileOutputStream(args[2]); // 2nd destination file

      int size = fis.available();
      int tocopy = size*3/4;

      int temp = 0, k ;
      while( ( k = ) != -1)
      if(temp <= tocopy) fos1.write(k); else fos2.write(k); temp++; } fos1.close(); fos2.close(); fis.close(); } }

  2. how can i read a specific row in a text file.
    example:in a text file they are 100 lines of text like this 10,sati,986780,
    how can i read a spefic line in a text file by using java

    plz tel me i am not getting

Leave a Comment

Your email address will not be published.