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;

/**
 * This class shows how to work properly with URL
 * @author The Developer's Info
 */
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/Bookmark

Related posts:

  1. Using Sequence Input Stream in Java
  2. Using URLConnection in Java
  3. Reading files in Java
  4. Using buffered streams in Java
  5. Using InputStream in Java

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>