Java One-Liner Scanner from URL Text File

In java, what code do I need to get from "http://www.mysite.com/text.txt" to a scanner that analyzes the received text contained on the site, as few lines as possible.

+3
source share
4 answers
URL yahoo = new URL("http://www.yahoo.com/");
    BufferedReader in = new BufferedReader(
                new InputStreamReader(
                yahoo.openStream()));

    String inputLine;

    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);

    in.close();

Link

+5
source
Scanner sc = new Scanner(new URL("http://www.mysite.com/text.txt").openStream());
+8
source

Taken from here , not verified

URLConnection connection = new URL("http://www.mysite.com/text.txt").openConnection();
String text = new Scanner(connection.getInputStream()).useDelimiter("\\Z").next();
+4
source

HTTP GET in one line of code: (using Java 8)

String doc = new Scanner(new URL(strUrl).openStream(), "UTF-8").useDelimiter("\\A").next();
0
source

All Articles