在Java中运行之间将数据存储在何处



我使用ObjectOutputStream和ObjectInputStream来读取和写入需要在程序运行之间存储的数据。我的问题是,是否有一个合适的地方来创建这些文件。目前,我使用以下代码来查找用户的Documents文件夹。

String myDocuments = null;
try {
Process p = Runtime.getRuntime()
.exec("reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v personal");
p.waitFor();
InputStream in = p.getInputStream();
byte[] b = new byte[in.available()];
if (in.read(b) == 0) myDocuments = JOptionPane.showInputDialog(this,
"A fatal error has occurred. n Please provide your documents folder.");
else {
myDocuments = new String(b);
myDocuments = myDocuments.split("\s\s+")[4];
}
in.close();
Log.logLine(myDocuments);
} catch (Throwable t) {
t.printStackTrace();
}

这可以作为一种练习吗?如果不能,我应该把这些信息存储在哪里?还有一种方法可以做到这一点,同时仍然允许应用程序运行不同的操作系统,因为我知道当前的方法只适用于Windows操作系统。

我想这取决于您的需求。每个操作系统都应该使用应用程序自己的文件夹(或其子文件夹(:

String dir = System.getProperty("user.dir");

最新更新