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保存获取Web内容的文件的更多相关文章
- java ,js获取web工程路径
一.java获取web工程路径 1),在servlet可以用一下方法取得: request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/” ...
- java获取web项目下文件夹的路径方法
方法一: String realPath=request.getSession().getServletContext() .getRealPath("upload"); 方法二: ...
- Java:<获取>、<删除>指定文件夹及里面所有文件
工具类代码如下: 一.获取 public Class Test{ //定义全局变量,存放所有文件夹下的文档 List<String> fileList ; public List<S ...
- QT QSettings 操作(导入导出、保存获取信息)*.ini文件详解
1.QSettings基本使用 1.1.生成.ini文件,来点实用的代码吧. QString fileName;fileName = QCoreApplication::applicationDirP ...
- 关于获取web应用的文件路径的注意事项
今天在把数据写入文件时遇到了一个问题,指定的文件获取不到.一开始是这样的 URL url = XXX.class.getClassLoader().getResource(fileName);File ...
- java 保存和读取本地json文件
保存数据到本地文件 private void saveDataToFile(String fileName,String data) { BufferedWriter writer = null; F ...
- WKWebview 拼接tableview,获取web内容高度
- (void)addWKWebView { _webView = [[WKWebView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH, 0.1) ...
- java 动态获取web应用的部署路径
public static String DEPLOY_PATH = null; static { String CurrentClassFilePath = Constant.class.getRe ...
- [Phonegap+Sencha Touch] 移动开发77 Cordova Hot Code Push插件实现自己主动更新App的Web内容
原文地址:http://blog.csdn.net/lovelyelfpop/article/details/50848524 插件地址:https://github.com/nordnet/cord ...
随机推荐
- UIScrollViewA都PI得知。
//1.设定滚定条的样式 typedef NS_ENUM(NSInteger, UIScrollViewIndicatorStyle) { UIScrollViewIndicatorStyleDefa ...
- 设计模式 - 出厂模式(factory pattern) 详细说明
出厂模式(factory pattern) 详细说明 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27081511 工厂方法模式 ...
- IntelliJ IDEA15开发时设置中java complier 的问题
Error:java: Compilation failed: internal java compiler error set中java complier 设置的问题 ,项目中有人用jdk1.6 ...
- Hibernate HQL详细说明
1. Hibernate HQL详细说明 1.1. hql一个简短的引论 Hibernate它配备了一种非常强大的查询语言.这种语言看起来非常像SQL.但是不要 要对相位的语法结构似,HQL是很有 ...
- 在Linux下,在网络没有配置好前,怎样查看网卡的MAC地址?
在Linux下,在网络没有配置好前,怎样查看网卡的MAC地址? 使用 dmesg 与 grep 命令来实际,例如以下: [root@localhost ~]# dmesg | grep eth e10 ...
- C/C++数据对齐汇总
C/C++数据对齐汇总 这里用两句话总结数据对齐的原则: (1)对于n字节的元素(n=2,4,8,...),它的首地址能被n整除,才干获得最好的性能: (2)如果len为结构体中长度最长的变量,s ...
- 第三篇——第二部分——第二文 计划搭建SQL Server镜像
原文:第三篇--第二部分--第二文 计划搭建SQL Server镜像 本文紧跟上一章:SQL Server镜像简介 本文出处:http://blog.csdn.net/dba_huangzj/arti ...
- Tri_integral Summer Training 5 总结
比赛 题目 B D E G H I J 这是泰国的一场区域赛,除了C题英语非常抽以外,其余题目还不算难读. 一开场就发现了三道很水的题目,0:21:34就把三道题给过了,都是1A,赞Moor的手速. ...
- Visual Studio Team Services使用教程--默认团队checkin权限修改
- Windows Phone 8 应用内截图
WriteableBitmap wb = new WriteableBitmap(this.LayoutRoot, new MatrixTransform()); //wb.Render(this.L ...