-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weblog.java
27 lines (26 loc) · 1.08 KB
/
Weblog.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
// 5. Write a simple program to process web server logfiles.
import java.io.*;
import java.net.*;
public class Weblog {
public static void main(String[] args) {
try (FileInputStream fin = new FileInputStream(args[0]);
Reader in = new InputStreamReader(fin);
BufferedReader bin = new BufferedReader(in);) {
for (String entry = bin.readLine(); entry != null; entry = bin.readLine()) {
// separate out the IP address
int index = entry.indexOf(' ');
String ip = entry.substring(0, index);
String theRest = entry.substring(index);
// Ask DNS for the hostname and print it out
try {
InetAddress address = InetAddress.getByName(ip);
System.out.println(address.getHostName() + theRest);
} catch (UnknownHostException ex) {
System.err.println(entry);
}
}
} catch (IOException ex) {
System.out.println("Exception: " + ex);
}
}
}