Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm getting frustrated trying to find Java's equivalent to Groovy's

String content = "http://www.google.com".toURL().getText();

All I want to do is to read content from a URL into string. I don't want to pollute my code with buffered streams and loops for such a simple task. I looked into apache's HttpClient but I also don't see a one or two lines implementation...

share|improve this question
4  
Why not just create a utility class that encapsulates all that "polluted" buffered streams and loops? You could also use that class to handle things like the socket closing before the stream completes and to handle I/O blocks over a slow connection. After all, this is OO - encapsulate the functionality and hide it from your main class. – Jonathan B Dec 1 '10 at 20:31
1  
It cannot be done in one or two lines. – Thorbjørn Ravn Andersen Dec 1 '10 at 20:38

4 Answers

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);
 }
share|improve this answer
2  
You could rename the main method to, say getText, pass URL string as a parameter and have a one-liner: String content = URLConnectionReader.getText("http://www.yahoo.com/"); – Goran Jovic Dec 1 '10 at 20:27
Sure, that looks better. – Joseph Weissman Dec 1 '10 at 20:30
1  
Yup. A good old utility.. – Goran Jovic Dec 1 '10 at 20:33

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();
share|improve this answer
3  
Just don't forget you need to call Scanner#close() later. – Marcelo Dec 21 '12 at 3:55
What does the \\A do? – Ben McCann Jan 12 at 19:30
1  
This explains the \\A: weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner.html – ccleve Jan 15 at 21:21
Nice! Would you like me to CW my answer so that we can add this? – Joseph Weissman Jan 18 at 20:26
if the compiler gives a leak warning you should split the statement as here stackoverflow.com/questions/11463327/… – M.C. Apr 28 at 6:40
show 1 more comment

If you have the input stream (see Joe's answer) also consider ioutils.toString( inputstream ).

http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html#toString(java.io.InputStream)

share|improve this answer

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

share|improve this answer
+1 Thanks, this worked perfectly. One line of code AND it closes the stream! Note that IOUtils.toString(URL) is deprecated. IOUtils.toString(URL url, String encoding) is preferred. – gmale May 21 at 0:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.