How do I read / download webpage content?

You want to create a program that read a webpage content of a website page. The example below use the class to create a connection to the website. You create a new URL object and pass the url information of a page. After the object created you can open a stream connection using the openStream() method of the URL object.

Next, you can read the stream using the BufferedReader object. This reader allows you to read line by line from the stream. To write it to a file create a writer using the BufferedWriter object and specify the file name where the downloaded page will be stored.

When all the content are read from the stream and stored in a file close the BufferedReader object and the BufferedWriter object at the end of your program.

package; import*; import; import; public class UrlReadPageDemo { public static void main(String[] args) { try { System.setProperty("http.agent", "Chrome"); URL url = new URL(""); InputStreamReader in = new InputStreamReader(url.openStream()); FileWriter out = new FileWriter("kodejava.html"); BufferedReader reader = new BufferedReader(in); BufferedWriter writer = new BufferedWriter(out); String line; while ((line = reader.readLine()) != null) { System.out.println(line); writer.write(line); writer.newLine(); } reader.close(); writer.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

How do I get IP address of localhost?

The example here show you how to get an IP or host address using the class. To get an instance of InetAddress we call a static method of this class, the method is getLocalHost(), which return the local host address. Next, to get the IP address we can call the getHostAddress() method.



public class IPAddressExample {
    public static void main(String[] args) {
        try {
            InetAddress address = InetAddress.getLocalHost();
            String ip = address.getHostAddress();

            System.out.println("IP Address = " + ip);
        } catch (UnknownHostException e) {

The result of this code snippet:

IP Address =