简体   繁体   English

Java6中java.nio.file.Paths的替代方法

[英]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.

相关问题 如何在 nashorn javascript 中使用 Paths(java.nio.file.Paths) - How to use Paths(java.nio.file.Paths) in nashorn javascript Java.nio.file.Paths为当前目录提供了错误的路径? - Java.nio.file.Paths is giving incorrect path for current directory? JMeter - 在类'java.nio.file.Paths'中找不到静态方法get(java.lang.String) - JMeter - Static method get( java.lang.String ) not found in class'java.nio.file.Paths' java.nio.file.Paths - 如何在迭代时跳过无法访问的文件/文件夹 - java.nio.file.Paths - How to skip unaccessible file/folder while iterating 使用java.nio.file.Paths接口时缺少方案(IllegalArgumentException) - Missing scheme (IllegalArgumentException) while using java.nio.file.Paths interface 使用 java.nio.file.Paths & jsfml loadFromFile 时发生死锁 - Deadlock occurring when using java.nio.file.Paths & jsfml loadFromFile Java6中Java.nio.file.Path的替代品,用于OS文件系统灵活性 - Alternative to java.nio.file.Path in Java6 for OS File system Flexibility 复制文件时,JMeter Bean Shell采样器错误“在类'java.nio.file.Paths中找不到静态方法get(java.lang.String)” - JMeter Bean Shell Sampler error “…Static method get( java.lang.String ) not found in class'java.nio.file.Paths” when copying files java.nio.file的替代品 - Alternative to java.nio.file Java 6中java.nio.file.Files的替代方法 - Alternative to java.nio.file.Files in Java 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM