Reading Content from a Webpage in Java

Here, we will connect to a web page and read its contents. Connection to the address is handled by URLConnection object in library. We can manage reading of the page content by BufferedInputStream and InputStream objects in library.

Here is the code:

* Created by ismailsirma on 22.6.2015.
public class ReadWeb {

public static void main(String[] args){

try {
// create a URL object (uniform resource locator)
URL url = new URL("");

// Creating a url connection object using openConnection method located in
URLConnection uc = url.openConnection();
// adds a request property in order to talk in http with a web server
uc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
//Opens a communications link to the resource referenced by this URL

// create an input stream for connecting the file on web
InputStream stream = url.openStream();

// create a buffered input stream in order to read one or more bytes from the file
BufferedInputStream buffer = new BufferedInputStream(stream);

// create StringBuilder for collecting the data from file
// StringBuilder lets you append content or insert content into a string without rebuilding the string object
StringBuilder sb = new StringBuilder();

// loop through the file
while (true){
// In each loop, read a single character from the stream
// read method returns a single byte
int data =;
// in the end of the stream; read method returns the value of -1
if (data == -1){
break; // break out of the loop when the end of the file is reached
// In else statement, if end of the file is not reached, append the string
} else {

} catch(MalformedURLException e){ // Exception to indicate a malformed URL has occurred
// Either no legal protocol could be found in a specification string or the string could not be parsed
catch (IOException e) {


Share this Post


Featured Author