Reading URL Content into a String with Java
A common need in programming is to retrieve the contents of a URL and store them as a string. In Groovy, this task is simplified by the concise syntax:
String content = "http://www.google.com".toURL().getText();
However, finding an equivalent implementation in Java can prove to be more challenging. While Java provides multiple options for this task, many of them involve complex buffering and loop constructs.
Simplified Approach
Fortunately, since the initial request for a concise solution, Java has introduced a more straightforward approach:
String out = new Scanner(new URL("http://www.google.com").openStream(), "UTF-8").useDelimiter("\A").next();
This line utilizes the Scanner class to read the stream obtained from the URL, treating the entire stream as a single string.
Extended Implementation
If desired, a more complete implementation can be created as follows:
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 method takes a URL as an input and returns the corresponding string content.
The above is the detailed content of How to Read URL Content into a String with Java?. For more information, please follow other related articles on the PHP Chinese website!