Q:

What is the preferred way to read/write content from a URL in Java

What is the preferred way to open a connection to a website and then subsequently read the information on that page? There seem to be many specific questions about different parts, but no clear and simple examples. 

A:

Getting Text from a URL | Example Depot:
try {
    // Create a URL for the desired page
    URL url = new URL("http://hostname:80/index.html");

    // Read all the text returned by the server
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
        // str is one line of text; readLine() strips the newline character(s)
    }
    in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

When it comes to "writing" to a URL I suppose you would want something like Sending a POST Request Using a URL | Example Depot.

