0
import net.htmlparser.jericho.*;

@SuppressWarnings({ "serial", "unused" })
public class RenderToText extends JDialog {


    static JTextArea _resultArea = new JTextArea(100, 100);
    JScrollPane scrollingArea = new JScrollPane(_resultArea);
    private final static String newline = "\n";
    int filename = 100;
    String[] fileName = new String[filename];

    public RenderToText(){
        for (int i = 0; i < filename; i++) {
         String fileName = "abc"+i+".txt";
            // A File object to represent the filename
            File f = new File(fileName);
            f.delete();
        }


        _resultArea.setEditable(false);
        //Starting to write files
        try{
        FileReader fr = new FileReader(
                "C:\\Users\\user\\fypworkspace\\FYP\\Link\\abc.txt");
        BufferedReader textReader = new BufferedReader(fr);

        // for each URL, process the URL and render the HTML file
        int numberofURL = 100;
        String[] URL = new String[numberofURL];
        int a;




        // For each URL, assign one text file to store the contents

        // for each URL, extract the URL contents

        for (a = 0; a < numberofURL; a++) {
            for (int i = 0; i < numberofURL; i++) {

                URL[a] = textReader.readLine();
                try{
                try {
                    try {
                        // Render the text from the HTML file
                        String sourceUrlString = URL[a];


                        System.out.println("Using argument of \""
                                    + sourceUrlString + '"');


                        if (sourceUrlString.indexOf(':') == -1)
                            sourceUrlString = "file:" + sourceUrlString;
                        Source source = new Source(new URL(sourceUrlString));
                        String renderedText = source.getRenderer()
                                .toString();
                        _resultArea.append("\nSimple rendering of the HTML document:\n" + newline);
                        System.out.println(renderedText+ newline);

                        // Write the rendered text to a text file


                        String filename = ("abc" + i + ".txt");
                        Writer output = null;
                        String text = renderedText;
                        File file = new File(filename);
                        output = new BufferedWriter(new FileWriter(file));
                        output.write(text);
                        output.close();
                        System.out.println("Your file has been written"+ newline);



                        // Count the number of words available in the
                        // rendered text.

                        BufferedReader br = new BufferedReader(
                                new FileReader(
                                        "C:\\Users\\user\\fypworkspace\\FYP\\abc"
                                                + i + ".txt"));
                        String line = "", str = "";
                        int count = 0;
                        while ((line = br.readLine()) != null) {
                            str += line + " ";

                        }
                        StringTokenizer st = new StringTokenizer(str);
                        while (st.hasMoreTokens()) {
                            String s = st.nextToken();
                            count++;
                        }
                        _resultArea.append("File has " + count + " words."+ newline);
                    } catch (UnknownServiceException ex) {
                        System.out.println("The following url cannot be processed"+ newline);

                    }

                    System.out.println("\n");
                    System.out.println("\n");
                    System.out.println("\n");
                } catch (NullPointerException ex) {
                    System.out.println("End of URL");
                    System.exit(0);
                }
            }catch(IOException ex){
                System.out.println("The following url cannot be processed due to the need to login");
            }
            }
        }


    }catch (IOException e1) {
    }
    JPanel content = new JPanel();
    content.setLayout(new BorderLayout());
    content.add(scrollingArea, BorderLayout.CENTER);

    this.setContentPane(content);
    this.setTitle("TextAreaDemo B");

    this.pack();
    }



    public static void main(String[] args) throws IOException {
        JDialog win = new RenderToText();
        win.setVisible(true);

    }
    }

此代码提取网站的内容。我在输出上使用了 append,但是 jtextarea 没有出现。它无法运行,但无法将输出传输到 jtextarea 我错过了什么?

4

2 回答 2

1

您正在运行两个循环

for (a = 0; a < numberofURL; a++)
    for (int i = 0; i < numberofURL; i++)

每个步骤 100 步。因此,您正在从输入文件中读取 10000 个 URL。如果它们的数量不够,readline将返回 null ,因此您将出现异常(另请参阅externeon 的评论)。摆脱无用的第二个循环。

除了读取文件的错误之外,您的 textarea 还应该显示输出(在我的测试中也是如此)。因此,它似乎位于您的读取循环和其中的异常处理中。

注意:还请考虑来自外部的其他评论。

于 2011-04-03T11:26:55.983 回答
0

同意关于静态 JTextArea 的评论。如果您想剪切内容,您可以在不同位置添加的两个 JTextArea 中使用相同的 Document。

于 2011-04-03T10:55:47.510 回答