开发者

display console result on java GUI for each class (java newbie programmer)

guys..i has a simple question.

is it possible to display each class console result开发者_StackOverflow on java GUI ?

Each class has different console results..

import org.jsoup.Jsoup;

import 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;

public class SimpleWebCrawler { public static void main(String[] args) throws 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;

}

}

Example output :

Fetching 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


If you want to separate the standard output (System.out.println) based on which class it comes from, the answer is, no, (not easily at least).

I suggest you let each class that wants to do output get a PrintWriter as argument to the constructor, then use that PrintWriter instead of the System.out print writer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜