~/ learn/ comp-348/ cards/ Your first network program
1 of 6

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

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/87802cbf-ef29-4cc7-adf5-2b2143f80941/flashcard utf-8 LF