Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedReader not reading whole online XML file

public static void main(String[] args) throws MalformedURLException, IOException 
{
    // TODO code application logic here
    URL link1 = new URL("xmlFileHere");
    InputStream xml = link1.openStream();
    InputStreamReader reader = new InputStreamReader(xml);
    BufferedReader reader1 = new BufferedReader(reader);
    while(reader1.readLine()!= null)
    {
        System.out.println(reader1.readLine());
    }

}

Hello. As you can see my BufferedReader is not reading the whole online XML file, I don't know why. Any idea why this happens?
Thank you.

like image 428
Martin1993-03 Avatar asked Dec 02 '22 17:12

Martin1993-03


2 Answers

while(reader1.readLine()!= null)   // reading here
{
System.out.println(reader1.readLine());  // and here
}

You are skipping a line each time you loop...

Do,

String line=null;
while((line=reader1.readLine())!= null)   // reading here
{
System.out.println(line);  // and displaying here
}
like image 84
TheLostMind Avatar answered Dec 16 '22 01:12

TheLostMind


Consider using Scanner which thanks to hasNextLine() method makes iterating more intuitive.

Scanner scanner = new Scanner(reader1);
while (scanner.hasNextLine() ) {//check if next line exists
    System.out.println(scanner.nextLine());//use this next line
}
scanner.close();
like image 26
Pshemo Avatar answered Dec 16 '22 03:12

Pshemo