Reputation: 808
I've hosted a text file which I would like to load into a string using java.
My code doesn't seem to work producing errors, any help?
try {
dictionaryUrl = new URL("http://pluginstudios.co.uk/resources/studios/games/hangman/dictionary.dic");
} catch (MalformedURLException catchMalformedURLException) {
System.err.println("Error 3: Malformed URL exception.\n"
+ " Dictionary failed to load.");
}
// 'Dictionary' scanner setting to file
// 'src/Main/Dictionary.dic'
DictionaryS = new Scanner(new File(dictionaryUrl));
System.out.println("Default dictionary loaded.");
UPDATE 1: The file doesn't seem to load going to the catch. But the file exists.
Upvotes: 0
Views: 1648
Reputation: 114787
JavaDoc tells us:
File(URI uri)
Creates a new File instance by converting the given file: URI into an abstract pathname.
We can't create and use a File
instance for any other resource type (like http
).
Upvotes: 0
Reputation: 2027
Something like this should work in your case:
DictionaryS = new Scanner(dictionaryUrl.openStream());
Upvotes: 0
Reputation: 6543
You could do something that this tutorial does
public class WebPageScanner {
public static void main(String[] args) {
try {
URLConnection connection =
new URL("http://java.net").openConnection();
String text = new Scanner(
connection.getInputStream()).
useDelimiter("\\Z").next();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Upvotes: 1
Reputation: 2415
You need to use HttpClient and retrieve the data as a string or string buffer. then use parse or read as file.
Upvotes: 0