我在访问JavaFX项目内资源文件夹中存储的文本文件时遇到问题。我已经搜索过信息/解决方案,但是我一直在获取与使用FxmlLoader类访问fxml文件有关的问题的结果-这不是我的问题。
JavaFX项目结构
JavaFxResourceLoadingTest |____ Source Packages | |____ io.github.rkinson | |____ FileLoader.java | |____ ResourceLoadingTest.java | |____ StandardApplication.java |____ Resources |____ files |____ testFile.txt
StandardApplication.java 是一个简单的标准Java程序,可在JavaFX项目结构中运行。该程序具有与“健全性检查”版本相同的代码(请参见下文),但是该程序失败,并显示以下输出。
ant -f C:\\Users\\admin\\Documents\\Code\\JavaFxResourceLoadingTest jfxsa-run
init:
Deleting: C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\build\built-jar.properties
compile:
Detected JavaFX Ant API version 1.3
jfx-deployment:
jar:
Copying 12 files to C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run179079813
jfx-project-run:
Executing C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run179079813\JavaFxResourceLoadingTest.jar using platform C:\Program Files\Java\jdk1.8.0_191\jre/bin/java
File 1: file:\C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run179079813\JavaFxResourceLoadingTest.jar!\files\testFile.txt
File 2: file:\C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run179079813\JavaFxResourceLoadingTest.jar!\files\testFile.txt
File 3: file:\C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run179079813\JavaFxResourceLoadingTest.jar!\files\testFile.txt
File 4: file:\C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run179079813\JavaFxResourceLoadingTest.jar!\files\testFile.txt
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.nio.file.InvalidPathException: Illegal char <:> at index 4: file:\C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run179079813\JavaFxResourceLoadingTest.jar!\files\testFile.txt
at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
at java.io.File.toPath(File.java:2234)
at io.github.rkinson.ResourceLoadingTest.start(ResourceLoadingTest.java:46)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application io.github.rkinson.ResourceLoadingTest
Java Result: 1
Deleting directory C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run179079813
jfxsa-run:
BUILD SUCCESSFUL (total time: 2 seconds)
我知道这是例外;
原因:java.nio.file.InvalidPathException:索引4处的非法字符<:>:file:\ C:\ Users \ admin \ Documents \ Code \ JavaFxResourceLoadingTest \ dist \ run179079813 \ JavaFxResourceLoadingTest.jar!\ files \ testFile.txt
是由于文件路径无效引起的。
问题#1 -为什么在JavaFX项目中运行 StandardApplication.java 时会使用此文件路径;
file:\ C:\ Users \ admin \ Documents \ Code \ JavaFxResourceLoadingTest \ dist \ run179079813 \ JavaFxResourceLoadingTest.jar!\ files \ testFile.txt
此文件路径时;
当 StandardApplication.java (请参见下文)在其自己的Netbeans项目中作为单独的标准Java应用程序运行时,使用文件:C:\ Users \ admin \ Documents \ Code \ StandardResourceLoadingTest \ build \ classes \ files \ testFile.txt
吗?
ResourceLoadingTest.java 是一个简单的JavaFX应用程序,尝试使用两种略有不同的方法从资源文件夹中加载文件
ResourceLoadingTest.java
@Override
public void start(Stage primaryStage) {
Label fileContent = new Label("File load pending...");
StackPane root = new StackPane();
root.getChildren().add(fileContent);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Resource Loading Test");
primaryStage.setScene(scene);
primaryStage.show();
File file1 = new File(getClass().getResource("/files/testFile.txt").getFile());
File file2 = new File(ResourceLoadingTest.class.getResource("/files/testFile.txt").getFile());
System.out.println("File 1: " + file1.toString());
System.out.println("File 2: " + file2.toString());
FileLoader fileLoader = new FileLoader();
File file3 = fileLoader.loadFileInstance("/files/testFile.txt");
File file4 = FileLoader.loadFileStatic("/files/testFile.txt");
System.out.println("File 3: " + file3.toString());
System.out.println("File 4: " + file4.toString());
File[] files = {file1, file2, file3, file4};
for (File file : files) {
try (BufferedReader reader = Files.newBufferedReader(file.toPath())) {
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println("Content of file: '" + line + "'");
}
} catch (IOException ex) {
System.err.format("IOException: %s%n", ex);
}
}
}
FileLoader.java
public File loadFileInstance(String path) {
File file = new File(getClass().getResource(path).getFile());
return file;
}
public static File loadFileStatic(String path) {
File file = new File(FileLoader.class.getResource(path).getFile());
return file;
}
当我运行 ResourceLoadingTest.java 时,将产生以下输出。
ant -f C:\\Users\\admin\\Documents\\Code\\JavaFxResourceLoadingTest jfxsa-run
init:
Deleting: C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\build\built-jar.properties
compile:
Detected JavaFX Ant API version 1.3
jfx-deployment:
jar:
Copying 12 files to C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run174399269
jfx-project-run:
Executing C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run174399269\JavaFxResourceLoadingTest.jar using platform C:\Program Files\Java\jdk1.8.0_191\jre/bin/java
File 1: file:\C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run174399269\JavaFxResourceLoadingTest.jar!\files\testFile.txt
File 2: file:\C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run174399269\JavaFxResourceLoadingTest.jar!\files\testFile.txt
File 3: file:\C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run174399269\JavaFxResourceLoadingTest.jar!\files\testFile.txt
File 4: file:\C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run174399269\JavaFxResourceLoadingTest.jar!\files\testFile.txt
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.nio.file.InvalidPathException: Illegal char <:> at index 4: file:\C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run174399269\JavaFxResourceLoadingTest.jar!\files\testFile.txt
at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
at java.io.File.toPath(File.java:2234)
at io.github.rkinson.ResourceLoadingTest.start(ResourceLoadingTest.java:46)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application io.github.rkinson.ResourceLoadingTest
Java Result: 1
Deleting directory C:\Users\admin\Documents\Code\JavaFxResourceLoadingTest\dist\run174399269
jfxsa-run:
BUILD SUCCESSFUL (total time: 2 seconds)
我知道由于意外的文件路径而引发了相同的异常;
file:\ C:\ Users \ admin \ Documents \ Code \ JavaFxResourceLoadingTest \ dist \ run174399269 \ JavaFxResourceLoadingTest.jar!\ files \ testFile.txt
问题#2 为什么这个JavaFX项目试图使用此“意外”文件路径?
问题#3 :如何获取此JavaFX项目以访问存储在其中的文件
ResourceLoadingTest |____ Resources |____ files |____ testFile.txt
作为一个健全性检查,我可以想到最简单的文件访问项目,如下所示。这可以按预期工作:找到,加载,读取文件,并且文件内容显示在输出窗口中。
标准项目结构
StandardResourceLoadingTest |____ Source Packages | |____ io.github.rkinson | |____ StandardResourceLoadingTest.java |____ Resources |____ files |____ testFile.txt
单个类 StandardResourceLoadingTest.java 仅包含以下主要方法。
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
File file = new File(StandardResourceLoadingTest.class.getResource("/files/testFile.txt").getFile());
System.out.println("File: " + file.toString());
try (BufferedReader reader = Files.newBufferedReader(file.toPath())) {
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println("Content of file: '" + line + "'");
}
} catch (IOException ex) {
System.err.format("IOException: %s%n", ex);
}
}
在 Netbeans 中运行时,将产生以下输出。
文件:C:\ Users \ admin \ Documents \ Code \ StandardResourceLoadingTest \ build \ classes \ files \ testFile.txt 文件内容:“文件加载成功。”
与StackEdit一起写。