title: How to read and modify a text file based on some condition
body: <p>How can I read an existing text file and modify some of its value based on some condition like if I get some text like</p>
  
  <p>Apple 1 3 5</p>
  
  <p>Now, here whenever I get an integer value I have to increase its count by 1 and save back to the same file again.</p>
  
  <p>For example, if I read some content like above I should be able to save </p>
  
  <p>Apple 2 4 6 </p>
  
  <p>back to the same file again</p>
  
  <p>My code looks like </p>
  
  <p>try{
              FileWriter fstream = new FileWriter("c:\Apple.txt",true);</p>
  
  <pre><code>        FileReader inputFileReader   = new FileReader("c:\\Apple.txt");
          BufferedReader inputStream   = new BufferedReader(inputFileReader);
          PrintWriter    outputStream  = new PrintWriter(fstream);
  
  
  
          while((inline=inputStream.readLine())!=null)
          {
              values=inline.split("-");
  
              Integer l = Integer.parseInt(values[2])+1;
              outputStream.print((inline.replaceAll(values[2],l.toString())));
  
          }
  
          outputStream.close();
  
  
      }
  </code></pre>
  
  <p>So, I get an output like </p>
  
  <p>Apple 1 3 5 Apple 1 4 5</p>
  
  <p>But my required output is</p>
  
  <p>Apple 1 4 5</p>
  
  <p>Thanks in advace</p>