简体   繁体   English

Java - 跨平台文件路径

[英]Java - Cross-platform filepath

I'm trying to develop a cross-platform application that works on Desktop and Android as well using JavaFX and Gluon.我正在尝试使用 JavaFX 和 Gluon 开发一个可以在桌面和 Android 上运行的跨平台应用程序。

At runtime my code creates a serialized file in my resource folder.在运行时,我的代码在我的资源文件夹中创建了一个序列化文件。 I also need to read and write serialized data from/to it.我还需要从/向它读取和写入序列化数据。

I managed to work it on desktop, but not on android.我设法在桌面上工作,但不是在 android 上。 Because it have a different file structure I guess.因为我猜它有不同的文件结构。 That is why I try to get the file path dynamically.这就是我尝试动态获取文件路径的原因。

Existing resource files, which are created before runtime (and not modified) seems to works fine on both platform.在运行之前创建(且未修改)的现有资源文件似乎在两个平台上都可以正常工作。

I tried with new File("src/main/resources/folder/file.ser").getAbsolutePath();我尝试使用new File("src/main/resources/folder/file.ser").getAbsolutePath(); and by trying to access it from my root folder like this: getClass.getResources("/folder/file.ser").getPath();并尝试从我的根文件夹访问它,如下所示: getClass.getResources("/folder/file.ser").getPath(); . . Both of them works fine on desktop (Windows) but unfortunately Android does not find the file by file path.它们都可以在桌面(Windows)上正常工作,但不幸的是 Android 没有通过文件路径找到文件。

An other problem could be that I should not create runtime files in the resource folder but then where should I?另一个问题可能是我不应该在资源文件夹中创建运行时文件,但我应该在哪里创建?

Any idea how can I read and write runtime created files that works both on android and desktop?知道如何读取和写入在 android 和桌面上都可以使用的运行时创建的文件吗?

(If the information is not enough to help me, I try to reproduce my code in a minimal form and provide further details.) (如果信息不足以帮助我,我会尝试以最小形式重现我的代码并提供更多详细信息。)

I think you are on a completely wrong track.我认为你在一个完全错误的轨道上。 Creating or writing to files in the resource folder does not work in general.在资源文件夹中创建或写入文件通常不起作用。 The idea is that files in the resource folder get packaged into jar files or are otherwise bundled with an application and are not writable at runtime.这个想法是资源文件夹中的文件被打包成 jar 文件或以其他方式与应用程序捆绑在一起并且在运行时不可写。

What you should do is to create an application folder when your program is launched for the first time.您应该做的是在您的程序第一次启动时创建一个应用程序文件夹。 A common practice on desktop is for example to create an invisible folder ".myApp" in the users home directory.例如,桌面上的常见做法是在用户主目录中创建一个不可见的文件夹“.myApp”。 On other platforms like Android there are other platform specific naming and location rules, but the concept is the same.在 Android 等其他平台上,还有其他特定于平台的命名和位置规则,但概念是相同的。 At first launch time you can also copy necessary resources from your resource folder into this application folder so that you can edit them at runtime.在首次启动时,您还可以将资源文件夹中的必要资源复制到此应用程序文件夹中,以便您可以在运行时对其进行编辑。

Resource files with a path on the class path , could be packed in a jar and should be considered read-only, especially as resources might be cached in some cases.路径上具有路径的资源文件可以打包在 jar 中并且应该被视为只读的,尤其是在某些情况下资源可能会被缓存。 They are not File .它们不是File They can be captured by URL, URI, Path.它们可以通过 URL、URI、Path 来捕获。 The paths are case-sensitive and the path separator is / .路径区分大小写,路径分隔符为/

Hence resources can only be used as a template, an initial file.因此资源只能用作模板,一个初始文件。 They should be copied to a real File, outside the application.它们应该被复制到应用程序之外的真实文件中。

Path path = Paths.get(System.getProperty("user.home"), ".myapp/file.ser");
Files.createDirectories(path.getParent());
if (Files.exists(path)) {
    URL url = MyClass.class.getResource("/folder/file.ser");
    Path template = Paths.get(url.toURI());

    Files.copy(template, path);
}

Furthermore .ser , a serialized java object, is not a good idea.此外.ser ,一个序列化的 java 对象,不是一个好主意。 I would suggest XML using JAXB with annotations.我建议使用带有注释的 JAXB 的 XML。 More readable, maintainable, versionable.更具可读性、可维护性、可版本化。 No clash between development JRE at your place and deployed JRE at the client.在您的位置开发 JRE 与在客户端部署 JRE 之间没有冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM