日期:2014-05-20  浏览次数:20813 次

我想把下面这个程序中的两个System.out.println()括号输出数据存到本地文档中以便打印,该怎么弄呢?求完整的代码
package appgamerank;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
 *
 * @author dell
 */
public class AppGameRank {

  /**
  * @param args the command line arguments
  */
  public static void main(String[] args) {
  // TODO code application logic here
   
AppGameRank rh = new AppGameRank();
rh.saveHtml();
rh.getMsgfromHtml();
}

public void saveHtml(){

HttpClient client = new HttpClient();
String htmlurl = "http://www.appannie.com/top/iphone/united-states/games/";
System.out.println(htmlurl);
HttpMethod method = new GetMethod(htmlurl);
try
{
client.executeMethod(method);
System.out.println(method.getStatusLine());
String html = method.getResponseBodyAsString();
FileWriter fw = new FileWriter("D:\\Top Charts - iPhone - United States - Games App Annie.htm" );
fw.write(html);
fw.close();
} catch (HttpException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public void getMsgfromHtml(){

try
  {
  //URL url = new URL("http://www.appannie.com/top/iphone/united-states/games/");
  //Document doc = Jsoup.parse(url, 3000000);
  File f = new File("D:\\Top Charts - iPhone - United States - Games App Annie.htm");
  Document doc = Jsoup.parse(f,"UTF-8");
  Elements tables = doc.select("table");
  Element table = tables.get(1);
  Elements trs =table.getElementsByTag("tr");
  for(Element tr: trs)
  {
  Elements tds = tr.children();
  Element td = tds.get(2);//表示 Free那一列 
  Elements span =td.getElementsByTag("span");
  String content = span.get(0).html();
  if(content.contains("\u25b2"))
  {
  String up = content.replace("\u25b2", "");//正三角,倒三角是\u25bc
  int upnum = Integer.parseInt(up);
  if(upnum >=30)
  {
  Elements a = td.getElementsByTag("a");
   
  System.out.println(a.get(0).html());
   
   
  }
   
  }
  else if(content.contains("(new)"))
  {
  String up = content.replace("(new)","");
  Elements a = td.getElementsByTag("a");
  System.out.println(a.get(0).html());