我的妈妈呀!我等了N天了。怎么这里没人知道怎么去改这段代码吗? 搞笑来。这里是探讨和解疑技术问题的地方,不是免费troubleShooting的地方,不是技术支持。凭什么要人家给你改代码。把你的本月工资分给我一半,我就替你解决这个问题。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个问题不难解决啊,其实你用不着这么复杂的实现。我给你一段代码,可以直接编译运行,完全可以实现你的要求。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(); }} 非常感谢楼上的兄弟,不过这段代码我早已经写过。但是我觉得这段代码的效率不够高。http://community.csdn.net/Expert/topic/3156/3156164.xml?temp=.3702661我只是想看看java.nio.*和java.io.*之间比较看看java.nio.*的非堵塞在我的程序中能否起到提高效率作用。 你的代码应该先连接服务器www.163.com的80端口,然后对HTTP协议进行处理,也就是向SocketChannel发送数据GET /test/test.htm HTTP1.0\n\n,从服务器返回的数据也要处理HTTP协议. nio的非组赛不是这么起作用的,你只有一个thread和连接,都无所谓的。你看看Selector是怎么和这些东西关联上的,就会明白nio为什么高效了:)这个类提供了和select一样的功能 菜鸟求助 请问这句话怎么错了,应该怎么写 怎样获取系统list separator? java.lang.NullPointerException scjp 关于main的问题 请教网页游览器视频音频实现 求开源OA weblogic连接池支持blob吗? 我如何才能取到系统的时间呢?请手们指点。 请问JDNI怎么配置? 需要安装些什么东西 ? 关于多个线程结束的问题? 关于异常处理的问题,怎样把异常转换为用户能看懂的信息
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(); }
}
不过这段代码我早已经写过。
但是我觉得这段代码的效率不够高。
http://community.csdn.net/Expert/topic/3156/3156164.xml?temp=.3702661
我只是想看看java.nio.*和java.io.*之间比较
看看java.nio.*的非堵塞在我的程序中能否起到提高效率作用。
也就是向SocketChannel发送数据GET /test/test.htm HTTP1.0\n\n,
从服务器返回的数据也要处理HTTP协议.