Read url to string in few lines of java code Read url to string in few lines of java code java java

Read url to string in few lines of java code


Now that some time has passed since the original answer was accepted, there's a better approach:

String out = new Scanner(new URL("http://www.google.com").openStream(), "UTF-8").useDelimiter("\\A").next();

If you want a slightly fuller implementation, which is not a single line, do this:

public static String readStringFromURL(String requestURL) throws IOException{    try (Scanner scanner = new Scanner(new URL(requestURL).openStream(),            StandardCharsets.UTF_8.toString()))    {        scanner.useDelimiter("\\A");        return scanner.hasNext() ? scanner.next() : "";    }}


This answer refers to an older version of Java. You may want to look at ccleve's answer.


Here is the traditional way to do this:

import java.net.*;import java.io.*;public class URLConnectionReader {    public static String getText(String url) throws Exception {        URL website = new URL(url);        URLConnection connection = website.openConnection();        BufferedReader in = new BufferedReader(                                new InputStreamReader(                                    connection.getInputStream()));        StringBuilder response = new StringBuilder();        String inputLine;        while ((inputLine = in.readLine()) != null)             response.append(inputLine);        in.close();        return response.toString();    }    public static void main(String[] args) throws Exception {        String content = URLConnectionReader.getText(args[0]);        System.out.println(content);    }}

As @extraneon has suggested, ioutils allows you to do this in a very eloquent way that's still in the Java spirit:

 InputStream in = new URL( "http://jakarta.apache.org" ).openStream(); try {   System.out.println( IOUtils.toString( in ) ); } finally {   IOUtils.closeQuietly(in); }


Or just use Apache Commons IOUtils.toString(URL url), or the variant that also accepts an encoding parameter.