filereaderfilewriterreaderbuffered

Separate two processes on 1 txt: FileReader and FileWriter


In my code at the first step I write the result of the matcher to the file test1.txt. At the second step I read the content of test1.txt and use a part of speech tagger on it. My problem is that writing and reading to test1.txt works at the same time and that causes double entries in output.txt. How can I do it another way in order that reading from test1.txt begins only when writing to it ends and not at the same time?

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

        }

Solution

  •  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();
         }
        }
        }
    

    This is one of the options, you need to call the function with the reader after the writer is closed.

    A much better way is to use a single Thread for reader and writer each Here is an example how to synchronize two Threads: synchronize two threads in java