Here we go, another Java vs. Python comparison (I just can't help myself). This time it's about standard library usefulness in doing certain tasks. Fetching the contents of a URL should be a trivial one, but in Java, it's not. Especially if the contents of that URL are gzipped and use a nice charset such as UTF-8.
Java:
URL url = new URL("http://www.example.com/");
URLConnection conn = url.openConnection();
conn.connect();
InputStream in;
if (conn.getContentEncoding().equals("gzip")) {
in = new GZIPInputStream(conn.getInputStream());
}
else {
in = conn.getInputStream();
}
String charset = conn.getContentType();
BufferedReader reader;
if (charset.indexOf("charset=") != -1) {
charset = charset.substring(charset.indexOf("charset=") + 8);
reader = new BufferedReader(new InputStreamReader(in, charset));
}
else {
charset = null;
reader = new BufferedReader(new InputStreamReader(in));
}
StringBuilder builder = new StringBuilder();
String line = reader.readLine();
while (line != null) {
builder.append(line + '\n');
line = reader.readLine();
}
String content = builder.toString(); // FINALLY!
Python:
content = urllib2.urlopen("http://www.example.com/").read()
At first I though yeah, well, Java is probably older and wasn't designed to do such things very often. I was wrong. Java appeared in 1995, Python in 1991.