// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;

public class InsertLine {
   private static final String TEMPFILE = "__ZZZZ__.ZZZ";
   public static void insert(String oldfnm, int lineno, String html) throws IOException {
      BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(oldfnm)));
      PrintWriter pw = new PrintWriter(new FileOutputStream(TEMPFILE));
      String line;
      for(int i = 0; i < lineno; i++) {
         line = br.readLine();
         pw.println(line);
      }
      String[] htmlsplit = html.split("\n");
      for(int i = 0; i < htmlsplit.length; i++) {
         pw.println(htmlsplit[i]);
      }
      while((line = br.readLine()) != null) {
         pw.println(line);
      }
      pw.close();
      br.close();
      (new File(oldfnm)).delete();
      (new File(TEMPFILE)).renameTo(new File(oldfnm));
   }
   public static void main(String[] args) throws Exception {
      insert("C:\\z1.html", 5, "<li>1\n<li>2\n<li>3");
   }
}
