Save Web Page to a File in Java
Submitted by donbermoy on Monday, December 15, 2014 - 16:12.
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:
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.
4. To parse the content of the HTML file, we will use the DTD class and named it as html variable.
5. In the catch method, the source code below will trap the error.
Here's the full code of this tutorial:
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
- import java.io.*; //used to access the FileOutputStream,InputStream,and OutputStream class
- import java.net.*; // used to access the URL class
- import javax.swing.text.html.parser.DTD; //this library parses an html file to become a document
- try {
- OutputStream out = new FileOutputStream("source.html"); //filename of the html file after downloading
Close the InputStream and OutputStream.
- in.close(); // close the InputStream
- out.flush(); //flush the OutputStream
- out.close(); //close the OutputStream
- import java.io.*; //used to access the FileOutputStream,InputStream,and OutputStream class
- import java.net.*; // used to access the URL class
- import javax.swing.text.html.parser.DTD; //this library parses an html file to become a document
- public class saveWebPage{
- try {
- OutputStream out = new FileOutputStream("source.html"); //filename of the html file after downloading
- in.close(); // close the InputStream
- out.flush(); //flush the OutputStream
- out.close(); //close the OutputStream
- }
- }
- }
- }
Add new comment
- 130 views