Using URL in Java

Here I am going to show example which works with URL.
Each URL(Uniform Resource Locator) unambiguously identifies the location of a resource on the Internet.
Example shows how to connect to an URL,download its data and show it in console via System.out

import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;public class Main{ public static void main(String[] args) throws IOException{ InputStream inputStream = null; try{ URL u = new URL("http://thedevelopersinfo.com"); inputStream = u.openStream(); for (int c = inputStream.read();c != -1;c = inputStream.read()){System.out.write(c); }  } catch (MalformedURLException ex){ ex.printStackTrace(); } finally{ if (inputStream != null){inputStream.close(); }  }  }}

Download code from this post.

Share

No related posts.

Leave a Reply

  

  

  

You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.