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

    }
    }

このコードは、Webサイトのコンテンツを抽出します。出力にappendを使用していますが、jtextareaが出力されません。実行できませんが、出力をjtextareaに転送することはできません何が足りないのですか?

4

2 に答える 2

1

2 つのループを実行しています

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

それぞれ100ステップ。したがって、入力ファイルから 10000 個の URL を読み取ることになります。それらが十分にない場合はreadlinenull が返されるため、例外が発生します (extraneon からのコメントも参照してください)。無駄な 2 番目のループを取り除きます。

ファイルの読み取りエラーに加えて、テキストエリアに出力が表示されるはずです(私のテストでは表示されます)。したがって、読み取りループとその中の例外処理内にあるようです。

注: extraneon からの他のコメントも考慮してください。

于 2011-04-03T11:26:55.983 に答える
0

static JTextArea に関するコメントに同意します。コンテンツを共有したい場合は、異なる場所に追加された 2 つの JTextArea で同じ Document を使用できます。

于 2011-04-03T10:55:47.510 に答える