在 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");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章