Save Web Page to a File in Java

This tutorial will teach you how to create a program that can save a web page into a file using Java. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of saveWebPage.java. 2. Import the following package library:
  1. import java.io.*; //used to access the FileOutputStream,InputStream,and OutputStream class
  2. import java.net.*; // used to access the URL class
  3.  
  4. import javax.swing.text.html.parser.DTD; //this library parses an html file to become a document
3. We will have a try and catch method. In the try method, we will initialize variables in our Main, u as the URL that has http://www.sourcecodester.com/tutorials/java/8858/drag-and-drop-icon-java.html, out for the OutputStream, and in for the InputStream.
  1. try {
  2.  
  3. URL u = new URL("http://www.sourcecodester.com/tutorials/java/8858/drag-and-drop-icon-java.html"); //source of the html file
  4. OutputStream out = new FileOutputStream("source.html"); //filename of the html file after downloading
  5. InputStream in = u.openStream(); //reading the html file
4. To parse the content of the HTML file, we will use the DTD class and named it as html variable.
  1. DTD html = DTD.getDTD("html"); //parsing the document
  2. System.out.println(html.getName()); //get the name of type of document
Close the InputStream and OutputStream.
  1. in.close(); // close the InputStream
  2. out.flush(); //flush the OutputStream
  3. out.close(); //close the OutputStream
5. In the catch method, the source code below will trap the error.
  1. catch (Exception e) {
  2. System.err.println("Error reading web page");
  3. }
Here's the full code of this tutorial:
  1. import java.io.*; //used to access the FileOutputStream,InputStream,and OutputStream class
  2. import java.net.*; // used to access the URL class
  3.  
  4. import javax.swing.text.html.parser.DTD; //this library parses an html file to become a document
  5.  
  6. public class saveWebPage{
  7.  
  8. public static void main(String[] args) {
  9.  
  10. try {
  11.  
  12. URL u = new URL("http://www.sourcecodester.com/tutorials/java/8858/drag-and-drop-icon-java.html"); //source of the html file
  13. OutputStream out = new FileOutputStream("source.html"); //filename of the html file after downloading
  14. InputStream in = u.openStream(); //reading the html file
  15. DTD html = DTD.getDTD("html"); //parsing the document
  16. System.out.println(html.getName()); //get the name of type of document
  17. in.close(); // close the InputStream
  18. out.flush(); //flush the OutputStream
  19. out.close(); //close the OutputStream
  20. }
  21. catch (Exception e) {
  22. System.err.println("Error reading web page");
  23. }
  24.  
  25. }
  26.  
  27. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment