| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 
 |  
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
 
public class GetFile extends JApplet {
 
// java GetFile http://www.compupress.net or http://random.yahoo.com/bin/ryl
 
	public static void main(String[] arguments) {
		if ( arguments.length == 1 ) {
			PageFrame page = new PageFrame(arguments[0]);
			page.show();
		}
		else {
			System.out.println("Usage: java GetFile url");
		}
	}
}
 
class PageFrame extends JFrame {
	JTextArea box = new JTextArea("Get text...");
 
	URL page;
 
	public PageFrame (String address) {
		super(address);
		setSize( 600, 300 );
		JScrollPane pane = new JScrollPane(box);
		getContentPane().add(pane);
		WindowListener wl = new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				System.exit(0);
			}
		};
		addWindowListener(wl);
 
		try {
			page = new URL(address);
			getData(page);
		}
		catch (MalformedURLException e) {
			System.out.println("Bad URL: " + address );
		}
	}
 
	void getData( URL url ) {
		URLConnection conn = null;
		InputStreamReader in;
		BufferedReader data;
		String line;
		StringBuffer buf = new StringBuffer();
		try {
			conn = this.page.openConnection();
			conn.connect();
			box.setText("Connexion open...");
 
			in = new InputStreamReader(conn.getInputStream());
			data = new BufferedReader(in);
			box.setText("Reading data...");
 
			while ((line = data.readLine()) != null) {
				buf.append(line +"\n");
			}
			box.setText(buf.toString());
		}
		catch (IOException e) {
			System.out.println("I/O error: " + e.getMessage());
		}
	}
} | 
Partager