【问题标题】:Error with url.openStream() method when trying to read file from URL尝试从 URL 读取文件时 url.openStream() 方法出错
【发布时间】: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


【解决方案1】:

添加了import java.net.URL并删除了所有其他.net and URL imports

【讨论】:

    猜你喜欢
    • 2021-03-15
    • 1970-01-01
    • 2018-07-15
    • 2013-04-15
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 2019-07-31
    相关资源
    最近更新 更多