java编程 程序 在线等 要速度快
来源:百度知道 编辑:UC知道 时间:2024/06/17 08:42:10
这个是专帮你写的,应该没问题了,有什么不明白的可以再问我。
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class Test {
public static void main(String[] args) {
String url = "http://www.qq.com";//指定url
String myFile = "D://my.html";//指定你的路径(D盘)和文件名(my.html)
try {
URL u = new URL(url); //new URL对象
InputStream in = u.openStream();//从u中得到输入流
FileWriter fw = new FileWriter(myFile); //new FileWriter文件输出流对象
byte[] buf = new byte[255];//设定一个缓存区
while((in.read(buf,0,buf.length))!= -1) { //缓存区不断从in输入流中读数据
fw.write(new String(buf));//把数据写到文件中
}
fw.close(); //关闭输出流
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-