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

huangapple go评论71阅读模式
英文:

How to access data file in resource folder in IDE (eclipse)?

问题

以下是您提供的内容的翻译部分:

我正在学习关于 Lambdas 和 Streams 的 Java 8 教程。与其将数据文件添加到 src 文件夹中,我在项目中创建了一个 resources 文件夹,并将数据文件添加到其中。

尝试像这样访问会导致空指针异常。

private void exercise4() throws IOException, URISyntaxException {
  try (BufferedReader reader = Files.newBufferedReader(
      Paths.get(getClass().getResource(".\\resources\\SonnetI.txt").toURI()), StandardCharsets.UTF_8)) {
    /* 在这里填写您的代码 */
  }
}

这也会产生相同的错误。

Paths.get(getClass().getResource("resources/SonnetI.txt").toURI()), StandardCharsets.UTF_8)) {

我将 resources 文件夹添加到了构建路径,但是仍然出现相同的错误。

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)

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

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

英文:

I am doing the Lambdas and Streams Java 8 tutorial. Instead of adding the data file to the src folder, I created a resources folder in the project and added the data file to it.

Trying to access like this creates a Null Pointer Exception.

  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 */
    }
  }

This too, gets same error

    Paths.get(getClass().getResource("resources/SonnetI.txt").toURI()), StandardCharsets.UTF_8)) {

I added resources folder to the build path but same error.

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)

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

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

答案1

得分: 0

我查看了关于 Paths 的其他签名,这个 是有效的!

public static Path get(String first,
                       String... more)
将路径字符串或字符串序列连接起来形成路径字符串转换为 Path如果 more 没有指定任何元素则第一个参数的值是要转换的路径字符串如果 more 指定了一个或多个元素则每个非空字符串包括 first 在内都被视为名称元素的序列参见 Path),并连接起来形成路径字符串关于如何连接这些字符串的详细信息是特定于提供程序的但通常它们将使用名称分隔符作为分隔符进行连接例如如果名称分隔符是/”,并且调用 getPath("/foo", "bar", "gus")那么路径字符串/foo/bar/gus将被转换为 Path如果 first 为空字符串且 more 不包含任何非空字符串则返回表示空路径的 Path
通过调用默认 FileSystem 的 getPath 方法获取 Path

这个也很好用...

private void exercise4() throws IOException, URISyntaxException {
  try (BufferedReader reader = Files.newBufferedReader(
      Paths.get("resources","SonnetI.txt"), StandardCharsets.UTF_8)) {
    /* 在这里编写你的代码 */
  }
}
英文:

I looked at the other signatures for Paths and this one works!

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.

this works fine...

  private void exercise4() throws IOException, URISyntaxException {
    try (BufferedReader reader = Files.newBufferedReader(
        Paths.get("resources","SonnetI.txt"), StandardCharsets.UTF_8)) {
      /* YOUR CODE HERE */
    }
  }

huangapple
  • 本文由 发表于 2020年7月27日 21:21:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63116261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定