Writing Lucene StandardAnalyzer results to text file with OutputStreamWriter
- by user3693192
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();
}