在 1 个 txt 上分离两个进程:FileReader 和 FileWriter
Separate two processes on 1 txt: FileReader and FileWriter
在我的代码中,第一步我将匹配器的结果写入文件 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();
}
}
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();
runReader();
}
}
private void runReader(){
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();
}
}
}
这是其中一个选项,需要在writer关闭后用reader调用该函数。
一个更好的方法是为 reader 和编写器每个使用一个线程
下面是一个如何同步两个线程的例子:synchronize two threads in java
在我的代码中,第一步我将匹配器的结果写入文件 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();
}
}
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();
runReader();
}
}
private void runReader(){
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();
}
}
}
这是其中一个选项,需要在writer关闭后用reader调用该函数。
一个更好的方法是为 reader 和编写器每个使用一个线程 下面是一个如何同步两个线程的例子:synchronize two threads in java