使用序列化存储实例
Storing Instances with Serialization
我正在制作一个程序,需要保存对象以便将来检索。该程序将作为 jar 文件分发给不同的人。
当给对象 input/output 流一个绝对路径 (String
) 作为参数时,我已经可以存储和检索 类 的实例。
我还可以将图像和文本文件保存在资源文件夹中,并通过 getClass().getResource(String path)
.
将其作为资源获取
问题来了:
我已尽一切可能 save/get Objects to/from 资源文件夹。处理 URLS 和文件而不是普通的字符串真的很奇怪。有人可以帮帮我吗?我需要能够保存和检索相对于类路径的对象,以便当程序是保存在计算机上不同路径中的 jar 文件时,我可以访问这些对象。
1: 资源文件夹(在jar中),是只读的。
你可以在打包的时候创建数据,存入jar,但是之后,就完成了:只到read.
2:因此您希望用户可以读取和写入(并且它未嵌入到您的应用程序中)。
如果是个人资料,可以使用(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);
在我的电脑上,它给出:
C:\Users\develop2\AppData\Roaming
C:\Users\develop2\Local Settings\ApplicationData
C:\Users\develop2\AppData\Local
看到这个:What is the cross-platform way of obtaining the path to the local application data directory?
- 它适用于所有人,原则相同,但您可能会遇到安全问题
像这样:
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);
我正在制作一个程序,需要保存对象以便将来检索。该程序将作为 jar 文件分发给不同的人。
当给对象 input/output 流一个绝对路径 (String
) 作为参数时,我已经可以存储和检索 类 的实例。
我还可以将图像和文本文件保存在资源文件夹中,并通过 getClass().getResource(String path)
.
问题来了: 我已尽一切可能 save/get Objects to/from 资源文件夹。处理 URLS 和文件而不是普通的字符串真的很奇怪。有人可以帮帮我吗?我需要能够保存和检索相对于类路径的对象,以便当程序是保存在计算机上不同路径中的 jar 文件时,我可以访问这些对象。
1: 资源文件夹(在jar中),是只读的。
你可以在打包的时候创建数据,存入jar,但是之后,就完成了:只到read.
2:因此您希望用户可以读取和写入(并且它未嵌入到您的应用程序中)。
如果是个人资料,可以使用(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);
在我的电脑上,它给出:
C:\Users\develop2\AppData\Roaming
C:\Users\develop2\Local Settings\ApplicationData
C:\Users\develop2\AppData\Local
看到这个:What is the cross-platform way of obtaining the path to the local application data directory?
- 它适用于所有人,原则相同,但您可能会遇到安全问题
像这样:
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);