java缓存问题?

1.代码中出现了缓存报错
2.报错如下图

clipboard.png
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文本中
阅读 2.1k
1 个回答

/**
*字节流下载
*/
public static void getContext(){

    System.getProperties().setProperty("https.proxyHost",
            "proxy.tencent.com");
    System.getProperties().setProperty("https.proxyPort", "8080");
    String url = "https://www.dong12.cn";
    InputStream in=null;
    FileOutputStream os=null;
    try {
        URL realUrl = new URL(url);
        URLConnection connection = realUrl.openConnection();
        connection.connect();
        in = connection.getInputStream();
        os=new FileOutputStream(new File("E:/poem3.html"));
        byte[] b=new byte[1024];
        int c=0;
        while ((c=in.read(b))!=-1) {
            os.write(b,0,c);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        try {
            if(os!=null){
                os.close();
            }
            if(in!=null){
                in.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题