搞笑来。这里是探讨和解疑技术问题的地方,不是免费troubleShooting的地方,不是技术支持。凭什么要人家给你改代码。把你的本月工资分给我一半,我就替你解决这个问题。

解决方案 »

  1.   

    这个问题不难解决啊,其实你用不着这么复杂的实现。我给你一段代码,可以直接编译运行,完全可以实现你的要求。import java.io.*;
    import java.net.*;
    public class URLClient2 {
    protected URLConnection connection;
    public static void main(String[] args) {
    URLClient2 client = new URLClient2();
    String yahoo = client.getDocumentAt("http://www.163.com");
    System.out.println(yahoo);
    }public String getDocumentAt(String urlString) {
    StringBuffer document = new StringBuffer();
    try {
    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line = null; while ((line = reader.readLine()) != null){
    document.append(line + "\n");//读到文件
            //********************3
    File f=new File("163.txt");
                FileWriter fw=new FileWriter(f,true);//参数 true
                  fw.write(line);
                  fw.close();
    //********************3
            }reader.close();

    } catch (MalformedURLException e) {
    System.out.println("Unable to connect to URL: " + urlString);
    } catch (IOException e) {
    System.out.println("IOException when connecting to URL: " + urlString);
    }
    return document.toString(); }
    }
       
     
      

  2.   

    非常感谢楼上的兄弟,
    不过这段代码我早已经写过。
    但是我觉得这段代码的效率不够高。
    http://community.csdn.net/Expert/topic/3156/3156164.xml?temp=.3702661
    我只是想看看java.nio.*和java.io.*之间比较
    看看java.nio.*的非堵塞在我的程序中能否起到提高效率作用。
      

  3.   

    你的代码应该先连接服务器www.163.com的80端口,然后对HTTP协议进行处理,
    也就是向SocketChannel发送数据GET /test/test.htm HTTP1.0\n\n,
    从服务器返回的数据也要处理HTTP协议.
      

  4.   

    nio的非组赛不是这么起作用的,你只有一个thread和连接,都无所谓的。你看看Selector是怎么和这些东西关联上的,就会明白nio为什么高效了:)这个类提供了和select一样的功能