package com.kingt;

import java.awt.BufferCapabilities.FlipContents;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class JavaDemo {
//讀取網络資源
public String ReaderPageByUrl(String pageUrl){
URL url;
String pageString = "";
try {
url = new URL(pageUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String line = null;
while((line = br.readLine()) != null){
sb.append(line + "\n");
System.out.println(sb);
}
pageString = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return pageString;
}

//信息寫入文件
public void writeStringToFile(String filePath,String fileStr) throws IOException{
File file = new File(filePath);
FileOutputStream fileout = new FileOutputStream(file);
fileout.write(fileStr.getBytes());
fileout.close();
}

//生成靜態頁面
public void createStaticPage(String pageUrl,String filePath){
String pageStr = ReaderPageByUrl(pageUrl);
try {
writeStringToFile(filePath, pageStr);
} catch (Exception e) {
e.printStackTrace();
}
}

//測試
public static void main(String[] args) {
JavaDemo demo = new JavaDemo();
String pageUrl = "http://www.baidu.com";
String filePath = "C:/Documents and Settings/Administrator/桌面/db.txt";
try {
demo.createStaticPage(pageUrl, filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()