
为啥按视频敲完运行成功后没能创建index.html文件?
package com.sxt.intener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
package com.sxt.intener;
import java.net.MalformedURLException;
import java.net.URL;
public class TestURL {
public static void main(String[] args) throws MalformedURLException {
URL url=new URL("https://www.baidu.com:80/index.html#aa?username=bjsxt&pwd=bjsxt");
}
}
/**
* 爬虫
* 1)从网络获取资源www.baid.com
* 2)存储
*
*/
public class TestUrl2 {
public static void main(String[] args) throws IOException {
// 1)创建URL对象
URL url=new URL("http://www.baidu.com");
// 2)获取字节输入流
InputStream is=url.openStream();
// 3)缓冲流
BufferedReader br=new BufferedReader(new InputStreamReader(is,"utf-8"));
// 4)缓存到本地
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("index.html")));
// 5)边读边写
String line=null;
while ((line=br.readLine())!=null) {
bw.write(line);
bw.newLine();
bw.flush();
}
// 6)关闭
bw.close();
br.close();
}
}