|
3.8. ReviewWe've taken a very quick look at the syntax of Java statements, classes, and interfaces. Much of the syntax is very reminiscent of C, though Java's object-oriented features differ in significant ways from C++. We looked at how to put Java classes into packages, and at the implications of this for locating the .class files. We also showed what the HTML-based Javadoc documentation looks like. These HTML pages will likely be a handy reference for you as you design and write your Java code. Example 3.30. Single class example: FetchURLimport java.net.*; import java.io.*; public class FetchURL { private URL requestedURL; public FetchURL(String urlName) { try { requestedURL = new URL(urlName); } catch (Exception e) { e.printStackTrace(); } } public String toString() { String rc = ""; String line; BufferedReader rdr; try { rdr = new BufferedReader( new InputStreamReader( requestedURL.openConnection().getInputStream() ) ); while ((line = rdr.readLine()) != null) { rc = rc + line + "\n"; } } catch (Exception e) { e.printStackTrace(); rc = null; } return rc; } public static void main(String[] args) { int i; FetchURL f; for (i = 0; i < args.length; i++) { System.out.println(args[i] + ":"); System.out.println(new FetchURL(args[i])); } } } |
|