-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSourceViewer4.java
More file actions
31 lines (28 loc) · 871 Bytes
/
SourceViewer4.java
File metadata and controls
31 lines (28 loc) · 871 Bytes
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
import java.io.*;
import java.net.*;
public class SourceViewer4 {
public static void main (String[] args) {
try {
URL u = new URL(args[0]);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
try (InputStream raw = uc.getInputStream()) {
printFromStream(raw);
} catch (IOException ex) {
printFromStream(uc.getErrorStream());
}
} catch (MalformedURLException ex) {
System.err.println(args[0] + " is not a parseable URL");
} catch (IOException ex) {
System.err.println(ex);
}
}
private static void printFromStream(InputStream raw) throws IOException {
try (InputStream buffer = new BufferedInputStream(raw)) {
Reader reader = new InputStreamReader(buffer);
int c;
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}
}
}
}