我在写个小程序,需要过去网页代码的,我是菜鸟,请问我这个代码好吗?有不足的请指出!~~import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Scanner;public class URLReader
{
public static void main(String args[])
{
try
{
URL tirc = new URL("http://www.kkkmh.com/manhua/0609/mxo-mo-fa-xing-hui-mo-ai.html");
Scanner scan = new Scanner(tirc.openStream());
System.out.println(tirc.getHost());
while (scan.hasNext())
{
System.out.println(scan.next().replace("\\/", "/"));
}
}
catch (IOException e)
{
System.out.println(e);
}
} public static String china(String args)
throws UnsupportedEncodingException
{
String s = null;
s = new String(args.getBytes("UTF-8"), "Unicode");
return s;
}}
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Scanner;public class URLReader
{
public static void main(String args[])
{
try
{
URL tirc = new URL("http://www.kkkmh.com/manhua/0609/mxo-mo-fa-xing-hui-mo-ai.html");
Scanner scan = new Scanner(tirc.openStream());
System.out.println(tirc.getHost());
while (scan.hasNext())
{
System.out.println(scan.next().replace("\\/", "/"));
}
}
catch (IOException e)
{
System.out.println(e);
}
} public static String china(String args)
throws UnsupportedEncodingException
{
String s = null;
s = new String(args.getBytes("UTF-8"), "Unicode");
return s;
}}
Scanner scan = new Scanner(tirc.openStream(),"UTF-8")改成這樣就可以了
/** */
/**
* 读取一个网页全部内容
*/
public String getOneHtml(String htmlurl) throws Exception {
URL url;
String temp;
StringBuffer sb = new StringBuffer(); url = new URL(htmlurl);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream(), "utf-8"));// 读取网页全部内容
while ((temp = in.readLine()) != null) {
sb.append(temp);
}
in.close();
return sb.toString();
}
/** */
/**
* 读取一个网页全部内容
*/
public void getOneHtml(String htmlurl) throws Exception {
URL url;
String temp;
PrintWrite out =new PrintWrite(new File("D:/default.html"));
url = new URL(htmlurl);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream(), "utf-8"));// 读取网页全部内容
while ((temp = in.readLine()) != null) {
out.println(temp);
}
out.flush();
out.close();
in.close();
}我写的事D:/default.html
你可以随便制定路径