Java Reference
In-Depth Information
LISTING 17.1
Continued
10: public WebReader() {
11: super(“Get File Application”);
12: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
13: setSize(600, 300);
14: JScrollPane pane = new JScrollPane(box);
15: add(pane);
16: setVisible(true);
17: }
18:
19: void getData(String address) throws MalformedURLException {
20: setTitle(address);
21: URL page = new URL(address);
22: StringBuffer text = new StringBuffer();
23: try {
24: HttpURLConnection conn = (HttpURLConnection)
25: page.openConnection();
26: conn.connect();
27: InputStreamReader in = new InputStreamReader(
28: (InputStream) conn.getContent());
29: BufferedReader buff = new BufferedReader(in);
30: box.setText(“Getting data ...”);
31: String line;
32: do {
33: line = buff.readLine();
34: text.append(line + “\n”);
35: } while (line != null);
36: box.setText(text.toString());
37: } catch (IOException ioe) {
38: System.out.println(“IO Error:” + ioe.getMessage());
39: }
40: }
41:
42: public static void main(String[] arguments) {
43: if (arguments.length < 1) {
44: System.out.println(“Usage: java WebReader url”);
45: System.exit(1);
46: }
47: try {
48: WebReader app = new WebReader();
49: app.getData(arguments[0]);
50: } catch (MalformedURLException mue) {
51: System.out.println(“Bad URL: “ + arguments[0]);
52: }
53: }
54: }
 
Search WWH ::




Custom Search