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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3