gpt4 book ai didi

Java URL 文本文件转字符串

转载 作者:行者123 更新时间:2023-12-01 15:33:16 35 4
gpt4 key购买 nike

我托管了一个文本文件,我想使用 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com