Search code examples
javafilebrowsertemp

(JAVA) How open a temp htm file in browser?


I tried this, but without sucess:

         File temp = File.createTempFile("document", ".htm");

         BufferedWriter out = new BufferedWriter(new FileWriter(temp));
         out.write("hi !!!"); 
         out.close();

         System.out.println(temp.toURI().toString());


         java.awt.Desktop.getDesktop().browse(temp.toURI());

Solution

  • Works for me in slightly modified form:

        File temp;
        try {
            temp = File.createTempFile("document", ".htm");
            BufferedWriter out = new BufferedWriter(new FileWriter(temp));
            out.write("hi !!!");
            out.close();
    
            System.out.println(temp.toURI().toString());
    
            java.awt.Desktop.getDesktop().browse(temp.toURI());
        } catch (IOException e) {
            e.printStackTrace();
        }