title: Editing HTML file using Java
body: <p>I am able to parse the HTML file and make the required changes, but I am not able to store it as an output html file, ie, the changes are visible in console but not in the html file.
  My requirement is to take a html file as an input, edit it and then store the html file with the new changes. So far, all I am able to do is display the changes in the console.</p>
  
  <pre><code>package html_editer;
  import java.util.*;
  import org.jsoup.Jsoup;
  import org.jsoup.nodes.*;
  import org.jsoup.select.*;
  import java.io.*;
  
  public class editer
  {
  static String htmlLocation = "C:\\Desktop\\hello\\";               
  static String fileName = "x";
  static StringBuilder buildTmpHTML = new StringBuilder();
  static StringBuilder buildHTML = new StringBuilder();
  static String name = "Ankit";
  static String address = "India";
  static String phoneNumber = "1234567890";
  
  public static void main(String[] args)
  {
  
      readHTML(htmlLocation, fileName);
      modifyHTML();
  
      System.out.println(buildHTML.toString());
  
      buildTmpHTML.setLength(0);
      buildHTML.setLength(0);
  
      System.exit(0);
  }
  
      public static void readHTML(String directory, String fileName)
  {
      try
      {
  BufferedReaderbr=newBufferedReader
  (newFileReader(directory+fileName+".html"));
  
          String line;
          while((line = br.readLine()) != null)
          {
              buildTmpHTML.append(line);
          }
          br.close();
      }
      catch (Exception e)
      {
          e.printStackTrace();
          System.exit(1);
      }
  }
  
  
  public static void modifyHTML()
  {
      String htmld = buildTmpHTML.toString();
      Document doc = Jsoup.parse(htmld);
  
      final List&lt;TextNode&gt; nodesToChange = new ArrayList&lt;TextNode&gt;();
  
      NodeTraversor nd  = new NodeTraversor(new NodeVisitor() 
      {
        @Override
        public void tail(Node node, int depth) 
        {
          if (node instanceof TextNode) 
          {
            TextNode textNode = (TextNode) node;
            nodesToChange.add(textNode);
          }
        }
  
        @Override
        public void head(Node node, int depth) 
        {        
        }
      });
  
      nd.traverse(doc.body());
  
      for (TextNode textNode : nodesToChange) 
      {
        Node newNode = buildElementForText(textNode);
        textNode.replaceWith(newNode);
      }
  
      buildHTML.append(doc.html());
  }
  
  private static Node buildElementForText(TextNode textNode) 
    {
      String text = textNode.getWholeText();
      String[] words = text.trim().split(" ");
      Set&lt;String&gt; units = new HashSet&lt;String&gt;();
      for (String word : words) 
          units.add(word);
  
      String newText = text;
      for (String rpl : units) 
      {
          if(rpl.contains("Name"))
              newText = newText.replaceAll(rpl, "" + rpl + " " + name);
          if(rpl.contains("Address") || rpl.contains("Residence"))
              newText = newText.replaceAll(rpl, "" + rpl + " " + address);
          if(rpl.contains("Phone-Number") || rpl.contains("PhoneNumber"))
              newText = newText.replaceAll(rpl, ""+rpl+" "+phoneNumber);
      }
      return new DataNode(newText, textNode.baseUri());
    }}
  </code></pre>