英文:
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)
英文:
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)
答案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 */
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论