繁体   English   中英

将正在运行的 jar 文件中的 class 个对象写入另一个目录中的文件

[英]Write class objects from a running jar file to a file in another directory

基本上,我的程序在启动时从内存文件中的 class object 读取参数,如果运行程序中的参数发生更改,它会覆盖内存文件并再次从中读取以更新参数。

我的应用程序在 IDE 中正常工作。然后我从我的 IDE 构建了我的第一个 jar 并从批处理文件中运行它并且它可以工作,但不像预期的那样。

如果内存文件存在,它会在程序启动时毫无问题地读取。

但是当我尝试更改程序参数或启动没有内存文件的程序时,它应该用更新的 class object alt 覆盖内存文件。 创建一个新的,它返回“FileNotFoundException”。

这是我的业余代码,我创建了一个 class,目的是向/从文本文件写入/读取“SaveClass”object:

public class ManageMemory {
    //filepath
    private String MEMORY_DIR = new StringBuffer(System.getProperty("user.home"))
            .append("\\Documents\\memory.txt").toString();
    private File targetFile = new File (MEMORY_DIR);

    //writes selected object to txt-file" with exceptions included
    public void writeToMemory(SaveClass object) {
        try {
            FileOutputStream f = new FileOutputStream(MEMORY_DIR);
            ObjectOutputStream o = new ObjectOutputStream(f);
            //write object to file
            o.writeObject(object);

            o.close();
            f.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found while writing");
        } catch (IOException e) {
            System.out.println("Error initializing stream");
        }
    }

    //reads current object in memory directory
    public SaveClass readFromMemory() {
        SaveClass inMemory = new SaveClass();
        if (!targetFile.exists()) {
            writeToMemory(inMemory);
        }
        try {
            FileInputStream f = new FileInputStream(MEMORY_DIR);
            ObjectInputStream o = new ObjectInputStream(f);

            inMemory = (SaveClass) o.readObject();

        } catch (FileNotFoundException e) {
            System.out.println("File not found while reading");
        } catch (IOException e) {
            System.out.println("Error initializing stream");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return inMemory;
    }
}

我已经搜索了有关如何解决我的问题的信息,但没有找到我所理解的。 我测试了在运行 .jar 程序时在我的保存文件上打印 canWrite(),它返回了 true。

找出真正发生的事情的最佳方法是执行以下步骤:

  • 将 java.io.File 的所有用法替换为较新的路径class。
  • 将 FileOutputStream 的所有用法替换为Files.newOutputStream
  • 确保每个单独的catch块都打印堆栈跟踪。

java.io.File 是一个非常古老的 class。它有许多设计缺陷,仅仅是因为 API 的设计在 1995 年还没有被很好地理解。

但是java.nio.file package 更现代并且纠正了所有这些问题。 它还具有更详细和信息丰富的例外情况。

使用 package 看起来非常相似:

public void writeToMemory(SaveClass object) {
    try (ObjectOutputStream o = new ObjectOutputStream(
        Files.newOutputStream(
            Paths.get(MEMORY_DIR)))) {

        //write object to file
        o.writeObject(object);

    } catch (IOException e) {
        System.out.println("Error initializing stream");
        e.printStackTrace();
    }
}

这将打印一个异常,准确解释无法写入文件的原因。

(请注意,我使用的是try-with-resources语句——也就是说,ObjectOutputStream 的创建是在try之后的括号内——这将自动关闭 ObjectOutputStream,后者又将关闭底层的 OutputStream。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM