URl u=new URL("http://www.163.com");

解决方案 »

  1. import java.net.*;
    import java.io.*;/**
    *打开一个网页,并把内容保存在test.html里面
    *@author: Eastsun
    */
    public class ReadURL{
        public static void main(String[] args){
            URL url  =null;
            BufferedReader reader =null;
            FileWriter     writer =null;
            try{
                url = new URL("http://eastsun.javaeye.com");
                reader =new BufferedReader(new InputStreamReader(url.openStream()));
                writer =new FileWriter(new File("test.html"));
                String line =null;
                while((line =reader.readLine())!=null) writer.append(line);
            }catch(IOException e){
                e.printStackTrace();
            }finally{
                try{
                    if(reader!=null) reader.close();
                    if(writer!=null) writer.close();
                }catch(IOException ex){
                    System.out.println(ex);
                }
            }
        }
    }
      

  2. Java 6 里的方法if (Desktop.isDesktopSupported()) {
                    Desktop desktop = Desktop.getDesktop();
                    try {
                        URI uri = new URI("http://www.163.com");
                        desktop.browse(uri);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } catch (URISyntaxException ex) {
                        ex.printStackTrace();
                    }
                }
      

类似问题 »