[英]Alternative to java.nio.file.Paths in Java6
Since the java.nio.file.Paths class and its package do not exist in Java6 and Android, I was wondering how I could use an alternative method for Paths.get("filename").toUri().toURL()
? 由于Java6和Android中不存在java.nio.file.Paths类及其包,因此我想知道如何为
Paths.get("filename").toUri().toURL()
使用替代方法? I try to load a fxml
file using the FXMLLoader
in JavaFX. 我尝试使用JavaFX中的
FXMLLoader
加载fxml
文件。 Usually I put all fxml-files in a specific layout-package within the src directory, which makes it easy to load these files via relative path. 通常我将所有fxml文件放在src目录中的特定布局包中,这使得通过相对路径轻松加载这些文件变得容易。 However, because I build the JavaFX project for Android, I need to store the fxml-file into the
assets
directory so that it will automatically included in the apk file (using gradle build). 但是,因为我为Android构建了JavaFX项目,所以我需要将fxml文件存储到
assets
目录中,以便它将自动包含在apk文件中(使用gradle build)。
This is my project structure so far: 到目前为止,这是我的项目结构:
MyProject
-- src\main\java\somepackage\Main.java
-- assets\sample_layout.fxml
However, when I build the apk, the structure looks like this: 但是,当我构建apk时,其结构如下所示:
Content of MyProject.apk
-- assets\sample_layout.fxml
-- classes.dex // which contains all compiled classes
-- somepackage\Main.java
I need to access the sample_layout.fxml
within the assets
directory from the Main.java
. 我需要从
Main.java
访问assets
目录中的sample_layout.fxml
。 Any idea how I can do that? 知道我该怎么做吗?
Here is what is working on Java7, but not for Java6 (and not on Android since the Paths class is not available in Java6): 这是在Java7上有效的方法,但在Java6上无效 ( 在Android6上则不能,因为Paths类在Java6中不可用):
URL myFxmlLayout= Paths.get("assets/sample_layout.fxml").toUri().toURL();
FXMLLoader loader = new FXMLLoader(myFxmlLayout);
AnchorPane page = (AnchorPane) loader.load();
For Java6 I tried the following, which is not working: 对于Java6,我尝试了以下操作,但不起作用:
URL myFxmlLayout= new URI("file:///assets/sample_layout.fxml").toURL();
// OR
URL myFxmlLayout= new URI("file:///../../../../assets/sample_layout.fxml").toURL();
Hope you can help me. 希望您能够帮助我。
Best regards, Michael 最好的问候,迈克尔
In fact the usage of Paths was design-technically wrong to begin with. 实际上,一开始使用Paths在设计技术上是错误的。 The FXML source might better be packed in a jar as a read-only resource.
最好将FXML源打包为一个只读资源放在jar中。
MyProject
-- src\main\java\somepackage\Main.java
-- src\main\resources\assets\sample_layout.fxml
URL myFxmlLayout= Main.class.getResource("/assets/sample_layout.fxml");
In your original design, you could use new File(...)
instead, but then have to take care of the path to assets
( getAbsolutePath
), and the runtime installation of it. 在原始设计中,您可以改用
new File(...)
,但随后必须照顾到assets
的路径( getAbsolutePath
),以及它的运行时安装。
You can do 你可以做
new File(filename).toURI().toURL()
I haven't ever worked with JavaFX and Android, so I haven't tested this in the context you describe, but it is the pre- java.nio
version of the Paths.get(...)
idiom you use. 我从未使用过JavaFX和Android,因此尚未在您描述的上下文中对此进行测试,但这是您使用的
Paths.get(...)
惯用语的java.nio
版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.