/** * WebPageReader class * Given a URL, can return information about that page. **/ import java.net.*; import java.io.*; import java.util.*; public class WebPageReader { //// Fields private URL url; private URLConnection con; private BufferedReader stream; ///// Methods /** * Open with a URL **/ public WebPageReader(String s){ // Create the URL and the connection to it try { url = new URL(s); con = url.openConnection(); stream = new BufferedReader(new InputStreamReader(con.getInputStream())); } catch (Exception e) { System.out.println("An error opening the URL occurred."); System.out.println(e.getMessage());} } /** * A WebPageReader is ready to read if the stream is ready **/ public boolean readyToRead(){ try {return stream.ready();} catch (Exception e) {System.out.println("I/O error occurred."); System.out.println(e.getMessage()); return false;} } /** * The type of the material at the other end of the URL is * the contentType from the URLConnection **/ public String getType(){return con.getContentType();} /** * Next line is the next line from the material at the * other end of the URL. We read it like a file. * There is more material there as long as readyToRead() returns * true. We may also read a null when it's done. **/ public String nextLine(){ try {return stream.readLine();} catch (Exception e) {System.out.println("I/O error occurred."); System.out.println(e.getMessage()); return null;} } }