0

I am making a program that needs to save objects for retrieval at a future date. The program will be given out away as a jar file to different people.

I can already store and retrieve instances of classes when giving the Object input/output stream a absolute path (String) as a parameter. I can also save images and text files in the resources folder and get it as a resource with getClass().getResource(String path).

Here is the problem: I have tried every way possible to save/get Objects to/from the resources folder. It gets really weird dealing with URLS and Files and not ordinary Strings. Can someone please help me? I need to be able to save and retrieve objects relative to the classpath so that i can access the objects when the program is a jar file saved in different paths on the computer.

Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52
akarshkumar0101
  • 367
  • 2
  • 10
  • Consider the classpath as constituted from read-only jar files only. Files read and saved by the user shouldn't be loaded as resources by the class loader. They should be loaded as files, using file IO. Class.getResource() is useful for read-only assets embedded in the application (your application icon, splash screen, etc.) – JB Nizet Dec 13 '15 at 07:38

1 Answers1

0

1: resource folder (in jar), is read-only.

You can create datas, store in the jar when you package, but after, it is finished: only to read.

2: so you want user can read and write (and it is not embedded in your app).

  • if it is personal datas, you can use (for PC):

    String appdata= System.getenv("APPDATA");   
    
    System.out.println(appdata);
    
    
    String dataFolder = System.getProperty("user.home") + "\\Local Settings\\ApplicationData";
    
    System.out.println(dataFolder);
    
    
    String dataFolder2 = System.getenv("LOCALAPPDATA");
    

    System.out.println(dataFolder2);

on my PC, it gives:

C:\Users\develop2\AppData\Roaming

C:\Users\develop2\Local Settings\ApplicationData

C:\Users\develop2\AppData\Local

see this: What is the cross-platform way of obtaining the path to the local application data directory?

  • it is is for everybody, same principles, but you can encounter security issues

like this:

String programdata = System.getenv("PROGRAMDATA");
System.out.println(programdata);
String allusersprofile = System.getenv("ALLUSERSPROFILE");
System.out.println(allusersprofile); // same thing !
String publicdir = System.getenv("PUBLIC");
System.out.println(publicdir);
Community
  • 1
  • 1