读取url(1

时间:2023-03-08 22:45:13

就书本例子

import java.io.InputStream;
import java.net.URL; public class Test {
public static void main(String args[]){
try{
URL url1=new URL("http://www.baidu.com");
ReadURL read=new ReadURL();
read.seturl(url1);
Thread thread1=new Thread(read);
thread1.start();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
} class ReadURL implements Runnable{
private URL url;
void seturl(URL a){
url=a;
}
public void run(){
try{
InputStream in1=url.openStream();
byte []b=new byte[1024];
int n=-1;
for(;(n=in1.read(b,0,1024))!=0;){
String s=new String(b,0,n);
System.out.print(s);
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}