Here we will learn how to read from the URLConnection object. First get the URLConnection object from the URL object, and then get the read stream from the URLConnection object. The specific steps are as follows.
Source Code
package com.beginner.examples;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class ReadDataInURL {
public static void main(String[] args) throws Exception {
//Create a URL Object //
URL url = new URL("https://github.com/");
//Get the URL connection object
URLConnection connection = url.openConnection();
//Get the read flow from the connection object
InputStream reader = connection.getInputStream();
byte[] buff = new byte[1024];
int len = -1;
while((len=reader.read(buff))!=-1) {
System.out.print(new String(buff,0,len));
}
reader.close();
System.out.println("Done");
}
}
References
Imported packages in Java documentation: