Basic HTML version of Foils prepared May 12 1996

Foil 130 Accessing URL's in Java -- InputStreams and URLConnections

From CRPC Lectures on Java Language Applets Graphics CRPC Annual Meeting Tutorial -- May 14,1996. by Geoffrey C. Fox


String line; // Here is a useful example
try { URLConnection conn = npacurl.openConnection();
conn.connect(); // could set options after creation of conn in above line and before connect method invoked
InputStream in = conn.getInputStream(); // establish an InputStream on this connection
DataInputStream data = new DataInputStream(new BufferedInputSream(in)); // set up two filters allowing both buffering and line access to InputStream
while ((line = data.readline()) != null ) {
  • System.out.println("line"); } // print out line -- obviously put your favorite line processing here
} catch (IOException e) { process error }
Note one useful exception EOFException which can be caught and remove testing in read loops



Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Dec 14 1997