Java - Cross-platform filepath

我与影子孤独终老i 提交于 2021-01-29 20:18:51

问题


I'm trying to develop a cross-platform application that works on Desktop and Android as well using JavaFX and Gluon.

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. 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(); and by trying to access it from my root folder like this: 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.

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?

(If the information is not enough to help me, I try to reproduce my code in a minimal form and provide further details.)


回答1:


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.

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. On other platforms like Android there are other platform specific naming and location rules, but the concept is the same. 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.




回答2:


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. They are not File. They can be captured by 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. I would suggest XML using JAXB with annotations. More readable, maintainable, versionable. No clash between development JRE at your place and deployed JRE at the client.



来源:https://stackoverflow.com/questions/59680493/java-cross-platform-filepath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!