title: Java Program keeps inserting the same line into my file
body: <p>so I am trying to read a file and add two lines of code to it at the top. So far that hadn't been working so I tried just reading the lines and writing them back. This was a major failure. It only writes to one of the files in the directory and just keeps filling it with the xmlopentag even though its been commented out, over and over. If anyone has any ideas it would be appreciated.</p>
  
  <pre><code>ArrayList&lt;String&gt; lines = new ArrayList();
          BufferedReader reader = null;
          String xmlstylesheet = "&lt;?xml-stylesheet type=\"text/xsl\" href=\""+stylefilename+"\"?&gt;";
          String xmlopentag = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;";
          for(int i=0;i&lt;files.length;i++) {
              lines.clear();
              try {
                  reader = new BufferedReader(new FileReader(files[i]));
                  String text = null;
                  while ((text = reader.readLine()) != null) {
                      lines.add(text);
                  }
              } catch (FileNotFoundException ex) {
                  Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
              } catch (IOException ex) {
                  Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
              }
              //lines.add(1, xmlstylesheet);
              //lines.add(0, xmlopentag);
              try {
                  PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(files[i])));
                  for(int j=0;j&lt;lines.size();j++) {
                      writer.write(lines.get(i));
                  }
              } catch (IOException ex) {
                  Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
              }
          }
  </code></pre>