【发布时间】:2020-12-06 10:25:09
【问题描述】:
尝试使用 URL 类和 url.openStream() 从 URL 读取文件。
代码:
try {
URL url = new URL("http://www.puzzlers.org/pub/wordlists/pocket.txt");
Scanner s = new Scanner(url.openStream());
// read from your scanner
}
catch(IOException ex) {
// there was some connection problem, or the file did not exist on the server,
// or your URL was not in the right format.
// think about what to do now, and put it here.
ex.printStackTrace(); // for now, simply output it.
}
我收到一条错误消息:DocFlavor.URL 的 openStream iusundefined 方法。
我尝试了什么:
IDE 建议更改我所做的演员表 结果是:
URL url = new URL("http://www.puzzlers.org/pub/wordlists/pocket.txt");
Scanner s = new Scanner(((Object) url).openStream());
// read from your scanner
仍然是同样的错误。我也尝试将对象更改为 URL:
URL url = new URL("http://www.puzzlers.org/pub/wordlists/pocket.txt");
Scanner s = new Scanner(((URL) url).openStream());
// read from your scanner
请帮忙,谢谢!
【问题讨论】:
-
听起来你输入了错误的类。确保导入
java.net.URL。 -
@Slaw 你是神吗?谢谢!
标签: java file-handling