读取url(1

就书本例子

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());
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/vhyc/p/6092392.html