Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from URL

Status
Not open for further replies.

fatcodeguy

Programmer
Feb 25, 2002
281
CA
Hi,

I have an app that reads from several URLs consecutively. The first one always reads fully, but the following ones don't (Sometimes they do). It's like the app only reads about halfway and continues to the next url.

Anyone have any suggestions? Here's my code (the links i used are internal, so i haven't included them):

Code:
import java.io.*;
import java.lang.*;
import java.util.*;
import java.net.*;

public class Application{
       
       
       //main function
       public static void main (String args[]){
              
              Application.run();

       }//end main 
       
       public static void run(){
              String[] urls = {"","",""};            
              //buffered readers
              int ctr=0;
              InputStream is = null;
              BufferedReader in = null;
              for (ctr=0; ctr<urls.length;ctr++){
                  try 
                  {    
                      //vars
                      URL url = new URL(urls[ctr]);
                      is = url.openStream();
                      in = new BufferedReader(new InputStreamReader(is));
                      //CONNECT to URL
                      URLConnection urlConnection = url.openConnection();
                      
                      //READ content 
                      //in = urlConnection.getInputStream();
                      int availableSize = is.available();
                      char[] buf = new char[availableSize];
                      int readSize = in.read(buf,0,availableSize);
                      in.close();
                      is.close();
                      
                      //Variables to get/store doc info
                      System.out.println("***URL "+(ctr+1)+"/"+urls.length+": "+urls[ctr]);
                      String doc = new String(buf);    //whole document
                      System.out.println(doc);
                      System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++");
                  }
                  catch(MalformedURLException e)
                  {
                      System.err.println(ctr+" --> "+e.getMessage());
                  }
                  catch(IOException e)
                  {
                      System.err.println(ctr+" --> "+e.getMessage());
                  }
                  finally
                  {
                      
                  }
              }
       }

}//end class

Thanks!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top