在每个类的java GUI上显示控制台结果(java新手程序员)

时间:2011-03-17 11:55:54

标签: java

伙计们......我有一个简单的问题。

是否可以在java GUI上显示每个类控制台结果?

每个班级都有不同的控制台结果..

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)抛出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

1 个答案:

答案 0 :(得分:2)

如果你想根据它来自哪个类来区分标准输出(System.out.println),答案是,不,(至少不容易)。

我建议你让每个想要输出的类得到一个PrintWriter作为构造函数的参数,然后使用PrintWriter而不是System.out打印编写器。