我托管了一个文本文件,我想使用 java 将其加载到字符串中。
我的代码似乎无法运行并产生错误,有什么帮助吗?
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.");
更新1:该文件似乎没有加载到捕获处。但该文件存在。
最佳答案
你可以做 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();
}
}
}
关于Java URL 文本文件转字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9293802/