You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
993 B
Java

Read contents of URL
package com.ack.j2se.io;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class ReadContentsOfURL {
public static void main( String[] args ) throws Exception {
// create a URL object and open a stream to it
//URL ftpUrl = new URL("ftp://ftp.sun.com/welcome.msg");
URL httpUrl = new URL( "http://www.sun.com/index.html" );
InputStream istream = httpUrl.openStream();
// convert stream to a BufferedReader
InputStreamReader ir = new InputStreamReader( istream );
BufferedReader reader = new BufferedReader( ir );
// then read the contents of the URL through a BufferedReader
StringBuffer buf = new StringBuffer();
int nextChar;
while( ( nextChar = reader.read() ) != -1 ) {
buf.append( (char) nextChar );
}
// close the reader
reader.close();
System.out.println( buf );
}
}