Reading from the Web

In the File catting tutorial we saw how to plug together File objects and Reader objects to allow the contents of a file to be read. As it happens there are wrappers for URLs as well as wrappers for files. So let's adapt the file reading program to allow us to read web pages.

Reading from the Web

In the File catting tutorial we saw how to plug together File objects and Reader objects to allow the contents of a file to be read. As it happens there are wrappers for URLs as well as wrappers for files. So let's adapt the file reading program to allow us to read web pages.

Reading from the Web

In the File catting tutorial we saw how to plug together File objects and Reader objects to allow the contents of a file to be read. As it happens there are wrappers for URLs as well as wrappers for files. So let's adapt the file reading program to allow us to read web pages.

Reading from the Web

This program needs to use the Java network package

Reading from the Web

This program needs to use the Java network package

Reading from the Web

This program needs to use the Java network package

Reading from the Web

Instead of a File object, we need to create a URL object

Reading from the Web

Instead of a File object, we need to create a URL object

Reading from the Web

Instead of a File object, we need to create a URL object

Reading from the Web

Instead of a File object, we need to create a URL object

Reading from the Web

Instead of a File object, we need to create a URL object

Reading from the Web

A URL object can open a stream connection to its associated WWW page

Reading from the Web

A URL object can open a stream connection to its associated WWW page

Reading from the Web

A URL object can open a stream connection to its associated WWW page

Reading from the Web

InputStreams are the old-fashioned (pre Java 1.1) way of reading from a data source. Nowadays it is Readers that are much more International and UniCode-aware, so the InputStream must be wrapped by an InputStreamReader.

Reading from the Web

InputStreams are the old-fashioned (pre Java 1.1) way of reading from a data source. Nowadays it is Readers that are much more International and UniCode-aware, so the InputStream must be wrapped by an InputStreamReader.

Reading from the Web

InputStreams are the old-fashioned (pre Java 1.1) way of reading from a data source. Nowadays it is Readers that are much more International and UniCode-aware, so the InputStream must be wrapped by an InputStreamReader.

Reading from the Web

And that's the final code. The changes needed are very slight, and the asymmetry comes from a need to maintain backwards compatibility with non-internationalised code.

import java.io.*;

public class Cat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;

public class Cat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
*
public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		File f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		URL f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		URL f=new File(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		URL f=new URL(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		URL f=new URL(arg[i]);
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		URL f=new URL(arg[i]);
		*Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		URL f=new URL(arg[i]);
		InputStream is=f.openStream();
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		URL f=new URL(arg[i]);
		InputStream is=f.openStream();
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		URL f=new URL(arg[i]);
		InputStream is=f.openStream();
		Reader in=new FileReader(f);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		URL f=new URL(arg[i]);
		InputStream is=f.openStream();
		Reader in=new InputStreamReader(is);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}
import java.io.*;
import java.net.*;

public class NetCat  {

public static void main(String[] arg){
	for(int i=0; i<arg.length; i++){
	   try{
		URL f=new URL(arg[i]);
		InputStream is=f.openStream();
		Reader in=new InputStreamReader(is);
		
		int c;
		while((c=in.read())!=-1)
			System.out.print((char)c);
		}
	    catch(IOException e){System.err.println(e);}
	    }
	}
}