Writing Lucene StandardAnalyzer results to text file with OutputStreamWriter

Posted by user3693192 on Stack Overflow See other posts from Stack Overflow or by user3693192
Published on 2014-05-31T00:43:40Z Indexed on 2014/05/31 9:26 UTC
Read the original article Hit count: 198

Filed under:
|

I'm getting ONLY the last result written to "outputStreamFile.txt". Can't figure out how to revise code so I can get ALL results written to text file.

Sample input text: "1st line of text\n" "2nd line of text \n"

Results in only 2nd line begin written (and not 1st line) as: "2nd line text\n"

private static void analyze(String text) throws IOException {
    analyzer = new StandardAnalyzer(Version.LUCENE_30);

    Reader r = new StringReader(text);
    TokenStream ts = (TokenStream) analyzer.tokenStream("", r);

    TermAttribute term = ts.addAttribute(TermAttribute.class);

    File outfile = new File("C:\\Users\\Desktop\\outputStreamFile.txt");
    FileOutputStream fileOutputStream = new FileOutputStream(outfile);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF8");

    while(ts.incrementToken()) {
        //System.out.print(term.term() + " "); 
        outputStreamWriter.write(term.term().toString() + "\r\n");
    }
    outputStreamWriter.close();
}

© Stack Overflow or respective owner

Related posts about lucene

Related posts about output