/** Open up a URL, and read the input. * As a side-effect (for demonstration only), print each line to the console. * @return The number of lines in a given URL. */ public static int countLines() throws java.net.MalformedURLException, java.io.IOException { final String sourcePage = "http://www.radford.edu/~itec120/2010fall-ibarland/" // "http://www.radford.edu" // "file:///Users/ian/120/index.html" ; // Open sourcePage for input: (Create a URL object based on sourcePage, open that URL as a stream-of-characters, // and create a Scanner who will group that stream's characters into Strings etc.) java.util.Scanner src = new java.util.Scanner( new java.net.URL( sourcePage ).openStream() ); int lineCount = 0; // Keep track of how many lines we've seen. String pageSoFar = ""; // All the lines of text seen so far. while ( src.hasNext() ) { pageSoFar = pageSoFar + src.nextLine(); lineCount = lineCount + 1; } System.out.println( "We read:\n" + pageSoFar ); return lineCount; }