-1

I'm building an application (for mac) that runs on java, and I need it to be able to edit its own contents. Because, after all, an application is just a glorified directory, I assumed that you could use it to change what is inside of it. But this application will be distributed, and I have no idea where the user will wish to place the application. Currently, all file references that I have been making have been using File("path/from/user/to/application") but of course, that assumes that you can find your application from the user home drive. Is there a way to do it not knowing where the .app file is stored? My current directory setup within the app is this:

app.app
Contents
|Info.plist
|PkgInfo
|---Resources
|   |icon.icns
|
|---MacOS
|   |JavaAppLauncher
|
|---Java
    |---app.jar
        |---package
            |classFile.class
            |---Folder
                |FileToEdit.xml

So is there a way to find and read/change FileToEdit.xml within the java code (stored in classFile.class) without knowing where app.app is?

zigzaugg
  • 61
  • 10
  • 2
    You might be able to get a "path" but there's no guarantee that it's backed up by a filesystem you can change. Java apps can be run from WAR files or read-only networks. Writing an app that works this way is guaranteed to be very brittle. – Jim Garrison Jul 23 '14 at 16:37
  • 1
    Data the application may change belongs probably belongs somewhere in `~/Library`, not the application itself. – chepner Jul 23 '14 at 16:45
  • What I'm trying to do is relatively simple. For example, I've seen some games on mac that store file data inside of the apps themselves. What I'm trying to do is similar to this. – zigzaugg Jul 24 '14 at 20:23

2 Answers2

0

I'd say all you need is to specify the path beginning from the src folder (looking from the project's folder perspective).

So if you have something like:

src/FileToEdit.xml

Then all you need to do is to use the /FileToEdit.xml since it's in the root of your class path.

See this: Read xml file inside my project (src)

Community
  • 1
  • 1
Breno Inojosa
  • 602
  • 1
  • 10
  • 20
  • I can do it just fine from within a project, but its quite a bit different inside of an application. File defaults to the user (~) after being launched, not a src folder. – zigzaugg Jul 23 '14 at 18:41
0

You can use app.class.getClassLoader().getResourceAsStream("package/FileToEdit.xml") to access your file within your program even if it's packed in a jar. As you can see, you don't need to use full path there, just make sure that your resource is included into build path.This is to read files though. There is no guarantee that the file will be writable.

OR

You can use temporary files to do what you need to do. Those are most likely be writable. You can use your original FileToEdit.xml file as a template for temp file and then create it with

File file = File.createTempFile("FileToEdit.xml", null);

and use that file.

But if you REALLY want to persist data, then you should use proper database.

dimoniy
  • 5,820
  • 2
  • 24
  • 22