-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathURLReader.java
39 lines (34 loc) · 1.06 KB
/
URLReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Anushka Dole & Mrida Yawale
// December 28, 2023
// Code to read URL
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.net.*;
public class URLReader {
private String url;
public URLReader(String url) {
this.url = url;
}
public String readURL() {
StringBuffer content = new StringBuffer();
try {
URL site = new URI(url).toURL();
URLConnection connection = site.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
content.append(line + "\n");
// System.out.println(line + "\n");
}
br.close();
} catch (Exception e) {
System.out.println("An exception occurred");
e.printStackTrace();
}
return content.toString();
}
}