1.代码中出现了缓存报错
2.报错如下图
3.代码如下:
import java.io.*;
import java.net.*;
public class TEXT{
public static void main(String[] args) throws IOException {
System.getProperties().setProperty("https.proxyHost", "proxy.tencent.com");
System.getProperties().setProperty("https.proxyPort", "8080");
// 定义即将访问的链接
String url = "https://www.dong12.cn";
// 定义一个字符串用来存储网页内容
String result = "";
// 定义一个缓冲字符输入流
BufferedReader in = null;
try {
// 将string转成url对象
URL realUrl = new URL(url);
// 初始化一个链接到那个url的连接
URLConnection connection = realUrl.openConnection();
// 开始实际的连接
connection.connect();
// 初始化 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
// 用来临时存储抓取到的每一行的数据
String line;
while ((line = in.readLine()) != null) {
//遍历抓取到的每一行并将其存储到result里面
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
FileOutputStream fos = new FileOutputStream("E:/poem3.txt") ;
DataOutputStream dos = new DataOutputStream(fos) ;
dos.writeUTF(result) ;
dos.close() ;
}
}
5.我该如何解决?
6.我需要抓取然后写入到txt文本中
/**
*字节流下载
*/
public static void getContext(){