A complete web client: open, chain, echo, close
A complete web client: open, chain, echo, close
Answer
URL page = new URL(args[0]); try (BufferedReader in = new BufferedReader( new InputStreamReader(page.openStream(), StandardCharsets.UTF_8))) { String line; while ((line = in.readLine()) != null) System.out.println(line); }
openStream() does the connecting; the reader names the charset; BufferedReader supplies readLine() and its own buffer, so a separate BufferedInputStream is optional here. Closing the outermost object closes the socket beneath it.
Harold 4e ch5 §Retrieving Data from a URL; ch2 §Filter Streams