首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

小弟我想把下面这个程序中的两个System.out.println()括号输出数据存到本地文档中以便打印,该如何弄呢?求完整的代码

2012-05-16 
我想把下面这个程序中的两个System.out.println()括号输出数据存到本地文档中以便打印,该怎么弄呢?求完整

我想把下面这个程序中的两个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());


  }
  }
   
   
  } catch (MalformedURLException e)
  {
  e.printStackTrace();
  } catch (IOException e)
  {
  e.printStackTrace();
  }


}

}



[解决办法]
getMsgfromHtml方法少复制了。。。这个才是完整的

Java code
    public void getMsgfromHtml() throws MalformedURLException, IOException {        PrintWriter out = null;        try {            out = new PrintWriter("D:/test.log");            // 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());                        println(out, 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());                    println(out, a.get(0).html());                }            }        } finally {            if (out != null) {                out.close();                out = null;            }        }    } 

热点排行