title: Modify the content of a file using Java
body: <p>I want to delete some  content of file using java program as below. Is this the write method to replace in the same file or it should be copied to the another file. </p>
  
  <p>But its deleting the all content of the file.</p>
  
  <pre><code>class FileReplace
  {
      ArrayList&lt;String&gt; lines = new ArrayList&lt;String&gt;();
      String line = null;
      public void  doIt()
      {
          try
          {
              File f1 = new File("d:/new folder/t1.htm");
              FileReader fr = new FileReader(f1);
              BufferedReader br = new BufferedReader(fr);
              while (line = br.readLine() != null)
              {
                  if (line.contains("java"))
                      line = line.replace("java", " ");
                  lines.add(line);
              }
              FileWriter fw = new FileWriter(f1);
              BufferedWriter out = new BufferedWriter(fw);
              out.write(lines.toString());
          }
          catch (Exception ex)
          {
              ex.printStackTrace();
          }
      }
      public statc void main(String args[])
      {
          FileReplace fr = new FileReplace();
          fr.doIt();
      }
  }
  </code></pre>