首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何访问IDE (eclipse)资源文件夹中的数据文件?

如何访问IDE (eclipse)资源文件夹中的数据文件?
EN

Stack Overflow用户
提问于 2020-07-27 13:21:59
回答 1查看 97关注 0票数 0

我正在做Lambdas和Streams Java 8教程。我没有将数据文件添加到src文件夹中,而是在项目中创建了一个资源文件夹,并将数据文件添加到其中。

尝试这样访问会创建一个空指针异常。

代码语言:javascript
复制
  private void exercise4() throws IOException, URISyntaxException {
    try (BufferedReader reader = Files.newBufferedReader(
        Paths.get(getClass().getResource(".\\resources\\SonnetI.txt").toURI()), StandardCharsets.UTF_8)) {
      /* YOUR CODE HERE */
    }
  }

这也会导致同样的错误

代码语言:javascript
复制
    Paths.get(getClass().getResource("resources/SonnetI.txt").toURI()), StandardCharsets.UTF_8)) {

我将“资源”文件夹添加到生成路径,但错误相同。

代码语言:javascript
复制
Exception in thread "main" java.lang.NullPointerException
    at lesson2.Lesson2.exercise4(Lesson2.java:96)
    at lesson2.Lesson2.runExercises(Lesson2.java:42)
    at lesson2.Lesson2.main(Lesson2.java:145)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-27 15:33:51

我查看了其他签名的路径和一个工作!

代码语言:javascript
复制
public static Path get(String first,
                       String... more)
Converts a path string, or a sequence of strings that when joined form a path string, to a Path. If more does not specify any elements then the value of the first parameter is the path string to convert. If more specifies one or more elements then each non-empty string, including first, is considered to be a sequence of name elements (see Path) and is joined to form a path string. The details as to how the Strings are joined is provider specific but typically they will be joined using the name-separator as the separator. For example, if the name separator is "/" and getPath("/foo","bar","gus") is invoked, then the path string "/foo/bar/gus" is converted to a Path. A Path representing an empty path is returned if first is the empty string and more does not contain any non-empty strings.
The Path is obtained by invoking the getPath method of the default FileSystem.

这个很好..。

代码语言:javascript
复制
  private void exercise4() throws IOException, URISyntaxException {
    try (BufferedReader reader = Files.newBufferedReader(
        Paths.get("resources","SonnetI.txt"), StandardCharsets.UTF_8)) {
      /* YOUR CODE HERE */
    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63116261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档