title: Reading a file and editing it in Java
body: <p>What I am doing is I am reading in a html file and I am looking for a specific location in the html for me to enter some text.
  So I am using a bufferedreader to read in the html file and split it by the tag . I want to enter some text before this but I am not sure how to do this. The html would then be along the lines of ...(newText)(/HEAD) (The brackets round head are meant to be angled brackets. Don't know how to insert them)</p>
  
  <p>Would I need a PrintWriter to the same file and if so, how would I tell that to write it in the correct location.
  I am not sure which way would be most efficient to do something like this.
  Please Help.</p>
  
  <p>Thanks in advance.</p>
  
  <hr>
  
  <p>Here is part of my java code:</p>
  
  <pre><code>        File f = new File("newFile.html");
          FileOutputStream fos = new FileOutputStream(f);
          PrintWriter pw = new PrintWriter(fos);
  
  
          BufferedReader read = new BufferedReader(new FileReader("file.html"));
          String str;
          int i=0;
          boolean found = false;
          while((str= read.readLine()) != null)
          {
  
              String[] data = str.split("&lt;/HEAD&gt;");
  
  
              if(found == false)
              {
                  pw.write(data[0]);
                  System.out.println(data[0]);
                  pw.write("&lt;/script&gt;");
                  found = true;
              }
              if(i &lt; 1)
              {
              pw.write(data[1]);
              System.out.println(data[1]);
              i++;
              }
              pw.write(str);
              System.out.println(str);
  
  
          }
        }
        catch (Exception e) {
          e.printStackTrace( );
          }
  </code></pre>
  
  <p>When I do this it gets to a point in the file and I get these errors:</p>
  
  <p>FATAL ERROR: MERLIN: Unable to connect to EDG API,</p>
  
  <p>Cannot find .edg_properties file.,</p>
  
  <p>java.lang.OutOfMemoryError: unable to create new native thread,</p>
  
  <p>Cannot truncate table,</p>
  
  <p>EXCEPTION:Cannot open connection to server: SQLExceptio,</p>
  
  <p>Caught IOException: java.io.IOException: JZ0C0: Connection is already closed, ...</p>
  
  <p>I'm not sure why I get these or what all of these mean?</p>
  
  <p>please Help.</p>