在第一步的代码中,我将匹配器的结果写入文件 test1.txt。在第二步,我阅读了 test1.txt 的内容并在上面使用了词性标注器。我的问题是同时对 test1.txt 进行写入和读取,这会导致 output.txt 中出现重复条目。我怎样才能以另一种方式做到这一点,以便从 test1.txt 读取仅在写入结束时才开始,而不是同时开始?
while (matcher.find()) {
//create a new file and write to it during the search
try{
pWriter = new PrintWriter(new BufferedWriter(new FileWriter("E:/test/test1.txt", true)));//append any given input
pWriter.println(matcher.group()); //write the result of matcher to the new file
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (pWriter != null){
pWriter.flush();
pWriter.close();
}
}
System.out.println(matcher.group()); //Print the result of matcher to console
MaxentTagger tagger = new MaxentTagger("models/english-bidirectional-distsim.tagger");
FileInputStream fstream = new FileInputStream("E:/test/test1.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//pick up sentences line by line from the file test1.txt and store it in the string sample
while((sample = br.readLine())!=null)
{
//tag the string
String tagged = tagger.tagString(sample);
FileWriter q = new FileWriter("E:/test/output.txt",true);
BufferedWriter out =new BufferedWriter(q);
//write it to the file output.txt
out.write(tagged);
out.newLine();
out.close();
}
}