If you want to create an HTML file you need to write some HTML content into the file. It would be more nice if you name the output file with the .html suffix.
So after you get the file name from that JTextField
, you should add a .html suffix to it’s name and then use that to create the File.
After that have to write some html tags as Strings using that BufferedWriter bw
into the destination file. It creates it (if it does not exist) and writes the html tags into it:
String html = "<html> <head></head> <body></body> </html>";
bw.write(html);
bw.flush();
bw.close();
So you can add the above code under the following line:
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
and inside the try-catch
block.
Also typically you should close the streams in the finally
block, so you should add a finally
block under the catch
block and close bw
inside it. Also the bw.close()
may throw
a checked exception and you need to put a try-catch
inside the finally
block. Another good try is to use try with resource
if you are using java 7 or above.
Good Luck.
2
solved How can I create an Html file? [closed]