zoukankan      html  css  js  c++  java
  • java保存获取Web内容的文件

    package com.mkyong;
     
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
     
    public class GetURLContent {
    	public static void main(String[] args) {
     
    		URL url;
     
    		try {
    			// get URL content
    			url = new URL("http://www.mkyong.com");
    			URLConnection conn = url.openConnection();
     
    			// open the stream and put it into BufferedReader
    			BufferedReader br = new BufferedReader(
                                   new InputStreamReader(conn.getInputStream()));
     
    			String inputLine;
     
    			//save to this filename
    			String fileName = "/users/mkyong/test.html";
    			File file = new File(fileName);
     
    			if (!file.exists()) {
    				file.createNewFile();
    			}
     
    			//use FileWriter to write file
    			FileWriter fw = new FileWriter(file.getAbsoluteFile());
    			BufferedWriter bw = new BufferedWriter(fw);
     
    			while ((inputLine = br.readLine()) != null) {
    				bw.write(inputLine);
    			}
     
    			bw.close();
    			br.close();
     
    			System.out.println("Done");
     
    		} catch (MalformedURLException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
     
    	}
    }
  • 相关阅读:
    java
    GO学习Day2
    GO学习Day1
    APS定时任务框架
    用微信每天给女朋友说晚安
    人生苦短,我用python
    Python 捕获terminate信号优雅关闭进程
    Python 多线程及多进程结合使用
    Python API 接口权限控制思路
    Docker runC 严重安全漏洞CVE-2019-5736 导致容器逃逸
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4889104.html
Copyright © 2011-2022 走看看