我加上stream.close()后我的程序不能停下来,一直在运行,这是为什么,上面的问题我好像知道了,不过还请高手指教

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;public class FileLister extends Frame {
    TextArea text;
    public FileLister(){
    addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){ dispose();}
    });text=new TextArea(" ",24,80);
    text.setFont(new Font("MonoSpaced",Font.PLAIN,12));
    text.setEditable(false);
    this.add("Center",text);Panel p=new Panel();
    p.setLayout(new FlowLayout(FlowLayout.RIGHT,10,5));
    this.add(p,"South");this.pack();}public void getUrl(){
    try{
    URL url=new URL("http://www.sina.com.cn/");
    String in;
    DataInputStream stream=new DataInputStream(url.openStream());
    while((in=stream.readLine())!=null){
    text.append(in);
    text.setCaretPosition(0);
    }
    }catch(MalformedURLException e){
    System.out.println("MalformedURLException:"+e);
    }catch(IOException e){
    System.out.println("IOException:"+e);
    }
    }public static void main(String [] args){
    FileLister f=new FileLister();
    f.addWindowListener(new WindowAdapter(){
    public void windowClosed(WindowEvent e){ System.exit(0);}
    });f.show();
    f.getUrl();
    }}
    此程序没问题给分吧
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;public class FileLister extends Frame {
    TextArea text;
    public FileLister(){
    addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){ dispose();}
    });text=new TextArea(" ",24,80);
    text.setFont(new Font("MonoSpaced",Font.PLAIN,12));
    text.setEditable(false);
    this.add("Center",text);Panel p=new Panel();
    p.setLayout(new FlowLayout(FlowLayout.RIGHT,10,5));
    this.add(p,"South");this.pack();}public void getUrl(){
    try{
    URL url=new URL("http://www.sina.com.cn/");
    String in;
    DataInputStream stream=new DataInputStream(url.openStream());
    while((in=stream.readLine())!=null){
    text.append(in);
    text.setCaretPosition(0);
    }
    }catch(MalformedURLException e){
    System.out.println("MalformedURLException:"+e);
    }catch(IOException e){
    System.out.println("IOException:"+e);
    }
    }public static void main(String [] args){
    FileLister f=new FileLister();
    f.addWindowListener(new WindowAdapter(){
    public void windowClosed(WindowEvent e){ System.exit(0);}
    });f.show();
    f.getUrl();
    }}
    此程序没问题给分吧
      

  3.   

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;public class FileLister extends Frame {
    TextArea text;
    public FileLister(){
    addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){ dispose();}
    });text=new TextArea(" ",24,80);
    text.setFont(new Font("MonoSpaced",Font.PLAIN,12));
    text.setEditable(false);
    this.add("Center",text);Panel p=new Panel();
    p.setLayout(new FlowLayout(FlowLayout.RIGHT,10,5));
    this.add(p,"South");this.pack();}public void getUrl(){
    try{
    URL url=new URL("http://www.sina.com.cn/");
    String in;
    DataInputStream stream=new DataInputStream(url.openStream());
    while((in=stream.readLine())!=null){
    text.append(in);
    text.setCaretPosition(0);
    }
    }catch(MalformedURLException e){
    System.out.println("MalformedURLException:"+e);
    }catch(IOException e){
    System.out.println("IOException:"+e);
    }
    }public static void main(String [] args){
    FileLister f=new FileLister();
    f.addWindowListener(new WindowAdapter(){
    public void windowClosed(WindowEvent e){ System.exit(0);}
    });f.show();
    f.getUrl();
    }}
    此程序没问题给分吧