URL java.net প্যাকেজের ক্লাস একটি ইউনিফর্ম রিসোর্স লোকেটারকে উপস্থাপন করে যা ওয়ার্ল্ড ওয়াইড ওয়েবে একটি সংস্থান (ফাইল বা, ডিরেক্টরি বা একটি রেফারেন্স) নির্দেশ করতে ব্যবহৃত হয়৷
ওপেনস্ট্রিম() এই শ্রেণীর পদ্ধতিটি বর্তমান বস্তু দ্বারা উপস্থাপিত URL-এর সাথে একটি সংযোগ খোলে এবং একটি ইনপুটস্ট্রিম অবজেক্ট প্রদান করে যা ব্যবহার করে আপনি URL থেকে ডেটা পড়তে পারেন।
অতএব, ওয়েব পৃষ্ঠা থেকে ডেটা পড়তে (ইউআরএল ক্লাস ব্যবহার করে) -
-
java.net.URL ক্লাসটি ইন্সট্যান্টিয়েট করুন কাঙ্খিত ওয়েব পেজের ইউআরএলটি প্যারামিটার হিসেবে এর কনস্ট্রাক্টরের কাছে দিয়ে।
-
OpenStream() পদ্ধতি ব্যবহার করুন এবং InputStream অবজেক্টটি পুনরুদ্ধার করুন।
-
উপরের পুনরুদ্ধার করা ইনপুটস্ট্রিম অবজেক্টটিকে প্যারামিটার হিসাবে পাস করে স্ক্যানার ক্লাসটি ইনস্ট্যান্ট করুন৷
উদাহরণ
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
public class ReadingWebPage {
public static void main(String args[]) throws IOException {
//Instantiating the URL class
URL url = new URL("http://www.something.com/");
//Retrieving the contents of the specified page
Scanner sc = new Scanner(url.openStream());
//Instantiating the StringBuffer class to hold the result
StringBuffer sb = new StringBuffer();
while(sc.hasNext()) {
sb.append(sc.next());
//System.out.println(sc.next());
}
//Retrieving the String from the String Buffer object
String result = sb.toString();
System.out.println(result);
//Removing the HTML tags
result = result.replaceAll("<[^>]*>", "");
System.out.println("Contents of the web page: "+result);
}
} আউটপুট
<html><body><h1>Itworks!</h1></body></html> Contents of the web page: Itworks!