Skip to content

Instantly share code, notes, and snippets.

@markjhoy
Last active September 7, 2023 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markjhoy/e22d3b3456d0ac4133685c013aa3f767 to your computer and use it in GitHub Desktop.
Save markjhoy/e22d3b3456d0ac4133685c013aa3f767 to your computer and use it in GitHub Desktop.
Read Text from URL in Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class DictionaryLoader {
public static List<String> loadDictionaryFromUrl() {
try {
URL url = new URL("https://gist.githubusercontent.com/mriley/036225a575a0a33d63e965d59bd036cb/raw/4ea9b2d36ca678fff601a445fdca2ca89a6c817f/dictionary.txt");
// read text returned by server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
List<String> ret = new ArrayList<String>();
while ((line = in.readLine()) != null) {
ret.add(line);
}
in.close();
return ret;
}
catch (MalformedURLException e) {
System.out.println("Malformed URL: " + e.getMessage());
}
catch (IOException e) {
System.out.println("I/O Error: " + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment