title: Using DataInputStream after BufferedReader
body: <p>A sample binary PGM file is as follows:</p>
  
  <pre><code>P5
  # This is a comment
  10 10
  255
  #image intensity information in bytes that I am unable to copy paste here
  </code></pre>
  
  <p>like</p>
  
  <p><a href="https://i.stack.imgur.com/knewG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/knewG.png" alt="enter image description here"></a></p>
  
  <p>When I try to read the file using the following code:</p>
  
  <pre><code>import java.io.*;
  
  public class Pgm_reader2 {
      public static void main(String[] args) throws IOException {
          try {
              FileInputStream inRaw = new FileInputStream("A.pgm");
              DataInputStream dis = new DataInputStream(inRaw);
              int i = 0;
              while(i &lt; 4){
                  System.out.println(dis.readLine());
                  i++;
              }
              while(dis.available() != 0){
                  System.out.print(dis.readUnsignedByte() + " ");
              }
          }catch (Exception e){
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>It works perfectly and gives the following output:</p>
  
  <pre><code>P5
  # This is a comment
  10 10
  255
  0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 0
  </code></pre>
  
  <p>but it shows that the <code>DataInputStream.readLine()</code> method is deprecated. So to avoid using this method I tried using the <code>BufferedReader.readLine()</code> method, as follows:</p>
  
  <pre><code>import java.io.*;
  
  public class Pgm_reader2 {
      public static void main(String[] args) throws IOException {
          try {
              FileInputStream inRaw = new FileInputStream("A.pgm");
              DataInputStream dis = new DataInputStream(inRaw);
              BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inRaw));
              int i = 0;
              while(i &lt; 4){
                  System.out.println(bufferedReader.readLine());
                  i++;
              }
              while(dis.available() != 0){
                  System.out.print(dis.readUnsignedByte() + " ");
              }
          }catch (Exception e){
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>And the output changed to:</p>
  
  <pre><code>P5
  # This is a comment
  10 10
  255
  </code></pre>
  
  <p>and it seems that the second <code>while</code> loop i.e. </p>
  
  <pre><code>while(dis.available() != 0){
      System.out.print(dis.readUnsignedByte() + " ");
  }
  </code></pre>
  
  <p>is not working.</p>
  
  <p>What could be the possible reason for this?</p>
  
  <p>I tried using a larger image i.e. 400x400 image instead of 10x10 image, 
  <a href="https://i.stack.imgur.com/YziIF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YziIF.png" alt="enter image description here"></a></p>
  
  <p>and tried to copy the bytes from one image and paste them into the new file using the following code:</p>
  
  <pre><code>import java.io.*;
  
  public class Pgm_reader2 {
      public static void main(String[] args) throws IOException {
          try {
              FileInputStream inRaw = new FileInputStream("A.pgm");
              FileOutputStream outRaw = new FileOutputStream("B_test.pgm");
              DataInputStream dis = new DataInputStream(inRaw);
              DataOutputStream dos = new DataOutputStream(outRaw);
              BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inRaw));
              String line = null;
              int i = 0;
              while(i &lt; 4){
                  line = bufferedReader.readLine();
                  dos.writeBytes(line);
                  dos.writeBytes("\n");
                  i++;
              }
              int intbyte = 0;
              while(dis.available() != 0){
                  intbyte = dis.readUnsignedByte();
                  dos.writeByte(intbyte);
              }
          }catch (Exception e){
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>and the output image came out like this:</p>
  
  <p><a href="https://i.stack.imgur.com/jtU9U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jtU9U.png" alt="enter image description here"></a></p>
  
  <p>If I use <code>DataInputStream.readLine()</code> in the whole code then the output image comes exactly the same as the input image. How is it happening?</p>