You are on page 1of 2

EX-NO:

AIM:

To perform a web page downloader program using java.

PROCEDURE:

Steps:
1. Create a URL object and pass url as string to download the webpage.
URL example = new URL(pass url of webpage you want to download)
2. Create Buffered Reader object and pass openStream(). Method of URL in Input Stream
object.
3. Create a string object to read each line one by one from stream.
4. Write each line in html file where webpage will be downloaded.
5. Close all objects.
6. Catch exceptions if url failed to download.
PROGRAM:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
public class Main {
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.google.com");
BufferedReader reader = new BufferedReader(new
InputStreamReader(url.openStream()));
BufferedWriter writer = new BufferedWriter(new FileWriter("data.html"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
writer.write(line);
writer.newLine();
}
reader.close();
writer.close();
}
}

OUTPUT:
<!DOCTYPE html>
<!--[if IE 8]><html class="ie ie8"> <![endif]-->
<!--[if IE 9]><html class="ie ie9"> <![endif]-->
<!--[if gt IE 9]><!--> <html> <!--<![endif]-->
<head>
<!-- Basic -->
<meta charset="utf-8">
<title>Java Examples - Socket to a single client</title>
<meta name="description" content="Java Examples Socket to a single client : A
beginner's tutorial containing complete knowledge of Java Syntax Object Oriented
Language, Methods, Overriding, Inheritance, Polymorphism, Interfaces, Packages,
Collections, Networking, Multithreading, Generics, Multimedia, Serialization, GUI." />
<meta name="keywords" content="Java, Tutorials, Learning, Beginners, Basics,
Object Oriented Language, Methods, Overriding, Inheritance, Polymorphism,
Interfaces, Packages, Collections, Networking, Multithreading, Generics, Multimedia,
Serialization, GUI." />
<base href="https://www.tutorialspoint.com/" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-
scalable=yes">
<meta property="og:locale" content="en_US" />
<meta property="og:type" content="website" />
<meta property="fb:app_id" content="471319149685276" />
<meta property="og:site_name" content="www.tutorialspoint.com" />
<meta name="robots" content="index, follow"/>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="author" content="tutorialspoint.com">
<script type="text/javascript" src="/theme/js/script-min-v4.js"></script>

RESULT:
Thus the above web page downloader has been executed and implemented successfully.

You might also like