Search code examples
javabufferedreader

Missing first character in many lines while from BufferedReader


I am trying to print the contents of a URL but the first character goes missing in many lines. Like in the following output "<" is missing in DOCTYPE, head and html tag.

I have tried using readLine method in while loop but it displays a more weird output with DOCTYPE, head tags missing and null in the end.

Code1:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class urlConnect {
public static void main(String[] args) throws Exception {
     URL urlObj = new URL("https://soundcloud.com");
     URLConnection conn = urlObj.openConnection();
     BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
     while ((br.read()) != -1) {
     System.out.println((br.readLine()));
     }
 }
}

Output1:

!DOCTYPE html>
<html lang="en">
head>
 <meta charset="utf-8">
 
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  
...
...
...
</body>
/html>

Code2:

while ((br.readLine()) != null) {
System.out.println((br.readLine()));
 }

Output2:

<head>
  

  <link rel="dns-prefetch" href="//style.sndcdn.com">
  <link rel="dns-prefetch" href="//api-v2.soundcloud.com">
  <link rel="dns-prefetch" href="//secure.quantserve.com">
  <link rel="dns-prefetch" href="//api.soundcloud.com">
  <link rel="dns-prefetch" href="//i1.sndcdn.com">
  <link rel="dns-prefetch" href="//i3.sndcdn.com">
  <link rel="dns-prefetch" href="//wis.sndcdn.com">
  <link rel="dns-prefetch" href="//pixel.quantserve.com">
  ...
  ...
  ...
     <script type="text/javascript">window.__sc_version = "1495803442";
    
  
    
</body>
null


Solution

  • Here's the javadoc of BufferedReader.read(), this is what it says:

    Reads a single character.

    Returns: The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached

    Now, let's have a look at the code:

    while ((br.read()) != -1) {
       System.out.println((br.readLine()));
    }
    

    You are checking the return value read to see if the end of stream has been reached. However, you are not printing that value (it is not stored in any reference and hence, it gets lost after comparison). Ideally, you need to print that value along with the whole line, e.g.:

    String line;
    while((line = br.readLine()) != null){
        System.out.println(line);
    }