首页 > 代码库 > java获取网页内容保存到文件
java获取网页内容保存到文件
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(); } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。