2011-03-17 27 views
-3

夥計..我有一個簡單的問題。顯示控制檯結果在每個類的Java GUI(java新手程序員)

是否可以在java GUI上顯示每個類控制檯的結果?

每個類都有不同的控制檯結果..

import org.jsoup.Jsoup; 

進口org.jsoup.helper.Validate; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements;

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL;

公共類SimpleWebCrawler { 公共靜態無效的主要(字串[] args)拋出IOException異常{

try { 
     URL my_url = new URL("http://theworldaccordingtothisgirl.blogspot.com/"); 
     BufferedReader br = new BufferedReader(new InputStreamReader(
       my_url.openStream())); 
     String strTemp = ""; 
     while (null != (strTemp = br.readLine())) { 
      System.out.println(strTemp); 
     } 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

    System.out.println("\n"); 
    System.out.println("\n"); 
    System.out.println("\n"); 

    Validate.isTrue(args.length == 0, "usage: supply url to crawl"); 
    String url = "http://theworldaccordingtothisgirl.blogspot.com/"; 
    print("Fetching %s...", url); 

    Document doc = Jsoup.connect(url).get(); 
    Elements links = doc.select("a[href]"); 

    System.out.println("\n"); 

    BufferedWriter bw = new BufferedWriter(new FileWriter("abc.txt")); 

    for (Element link : links) { 
     print(" %s ", link.attr("abs:href"), trim(link.text(), 35)); 

     bw.write(link.attr("abs:href")); 
     bw.write(System.getProperty("line.separator")); 
    } 
    bw.flush(); 
    bw.close(); 

} 

private static void print(String msg, Object... args) { 
    System.out.println(String.format(msg, args)); 
} 

private static String trim(String s, int width) { 
    if (s.length() > width) 
     return s.substring(0, width - 1) + "."; 
    else 
     return s; 

} 

}

輸出示例:

擷取http://theworldaccordingtothisgirl.blogspot.com/ ...

http://theworldaccordingtothisgirl.blogspot.com/2011/03/in-time-like-this.html
https://lh5.googleusercontent.com/-yz2ql0o45Aw/TYBNhyFVpMI/AAAAAAAAAGU/OrPZrBjwWi8/s1600/Malaysian-Newspaper-Apologises-For-Tsunami-Cartoon.jpg
http://ireport.cnn.com/docs/DOC-571892
https://lh3.googleusercontent.com/-nXOxDT4ZyWA/TX-HaKoHE3I/AAAAAAAAAGQ/xwXJ-8hNt1M/s1600/ScrnShotsDesktop-1213678160_large.png
http://theworldaccordingtothisgirl.blogspot.com/2011/03/in-time-like-this.html#comments
http://www.blogger.com/share-post.g?blogID=3284083343891767749&postID=785884436807581777&target=email
http://www.blogger.com/share-post.g?blogID=3284083343891767749&postID=785884436807581777&target=blog
http://www.blogger.com/share-post.g?blogID=3284083343891767749&postID=785884436807581777&target=twitter
http://www.blogger.com/share-post.g?blogID=3284083343891767749&postID=785884436807581777&target=facebook
http://www.blogger.com/share-post.g?blogID=3284083343891767749&postID=785884436807581777&target=buzz

+0

什麼是控制檯結果?你想要做的一個例子會很好。 – aioobe 2011-03-17 11:57:50

回答

2

如果你想在標準輸出分離(System.out.println)基於它來自哪個階層,答案是,不,(至少不容易)。

我建議你讓每個希望做輸出的類都得到一個PrintWriter作爲構造函數的參數,然後使用PrintWriter而不是System.out打印作者。