如何用java读取一个网站的源文件 保存到本地
2013-05-30 18:16
417 查看
http://zhidao.baidu.com/question/78956126.html
通过一个网址 读取网站的源代码
可是上面的代码会乱码
这样写就不会了
InputStreamReader inp =new InputStreamReader(url.openStream(),"UTF-8");
reader = new BufferedReader(inp);
通过一个网址 读取网站的源代码
可是上面的代码会乱码
这样写就不会了
InputStreamReader inp =new InputStreamReader(url.openStream(),"UTF-8");
reader = new BufferedReader(inp);
package com; import java.io.*; import java.net.URL; import java.text.SimpleDateFormat; import javax.swing.JTextArea; public class Newdownurl { static BufferedReader reader; static JTextArea tPage; static URL url; static String wenben; public static void main(String[] args) { tPage = new JTextArea(); SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd"); String datetime = tempDate.format(new java.util.Date()); System.out.println(datetime); String url = "http://test.tz2100.cn:8000/"; //网站路径 wenben="d:/tomcat_demo/webapps/cms_test///test/"+datetime+".shtml"; //存储路径 readPage(url); } public static void readPage(String uu) { String line; try { url = new URL(uu); InputStreamReader inp =new InputStreamReader(url.openStream(),"UTF-8"); reader = new BufferedReader(inp); while ((line = reader.readLine()) != null) { tPage.append(line + "\n"); } cunchu(); System.out.println("存储成功11"); } catch (Exception ie) { tPage.setText("发生输入输出异常 22"); } finally { try { if (reader != null) reader.close(); } catch (Exception e) { } } } public static void cunchu() { File f = new File(wenben); try { f.createNewFile(); FileWriter out = new FileWriter(f); out.write(tPage.getText()); out.flush(); // 清空管道 out.close(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- java通过一个url读取网站的源代码 储存到本地文件中
- java通过一个url读取网站的源代码 储存到本地文件中
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- java读取网络上的图片并保存到本地,图片文件名不变
- java读取本地图片和下载可选择路径保存图片
- 如何从js代码中读取传入的一个java对象的属性?
- 如何利用代码从网站上获取一个文件的大小,并且下载时不出现保存提示框
- [VB.NET]如何把一个图片保存到access中,又如何把它读取出来,并在picturebox中显示?
- 如何搭建一个本地网站
- 自定义字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,能够在读取的每行前面都加上有行号和冒号。
- Java从网络读取图片并保存至本地
- 如何优雅的爬取 gzip 格式的页面并保存在本地(java实现)
- 使用Java读取网络图片并保存至本地
- Java从网络读取图片并保存至本地
- java读取网络上的资源保存本地
- Java从网络读取图片并保存至本地
- Java--多线程读取网络图片并保存在本地
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 如何使用JavaExcel(jxl)读取一个文件并写入一个新文件
- 用thinkphp写的一个例子:抓取网站的内容并且保存到本地