title: Java: open text file as html file with browser using File.renameTo()
body: <p>I want to convert a text file to a html file and then open it with a browser. I try to use <code>file.renameTo()</code> to rename the text file's extension to <code>.html</code> but the rename attempt always fails and <code>file.renameTo()</code> always returns false. Therefore, when I try to open the file in the below method, the file is opened in notepad. </p>
  
  <p>file declaration:</p>
  
  <pre><code> private File file;
  </code></pre>
  
  <p>file declaration in constructor:</p>
  
  <pre><code> file = new File("D:/dc1000/Project/webPage.txt");
   file.getParentFile().mkdirs();
  </code></pre>
  
  <p>method that doesn't work:</p>
  
  <pre><code> public void compileWebpage(){
          File file2 = new File("D:/dc1000/Project/CompiledWebpage.html");
          file2.getParentFile().mkdirs();
          addFileTags("end"); //add ending tags like &lt;/body&gt;
          boolean success = true;
          try{
              success = file.renameTo(file2);
          }catch (Exception e){
              System.out.println(e);
          }
  
          if(!success){
            System.out.println("webPage compilation failed.");
          }
  
          Desktop desktop = Desktop.getDesktop();
          try{
              desktop.browse(file.toURI());
          }catch (IOException e){
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>No exceptions are thrown, "Webpage compilation failed" is printed to the console and then notepad opens the file. The file looks like this when opened in notepad: </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;p&gt;hi&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Why does <code>File.renameTo()</code> always fail? How can I open this text file in a browser as a html file?</p>