简体   繁体   English

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

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

Basically my program reads parameters from a class object in a memory-file at startup and if parameters are changed in the running program it overwrites memory-file and reads from it again to update parameters.基本上,我的程序在启动时从内存文件中的 class object 读取参数,如果运行程序中的参数发生更改,它会覆盖内存文件并再次从中读取以更新参数。

My application works as should in the IDE. Then I built my first jar from my IDE and ran it from a batch-file and it works, but not as expected.我的应用程序在 IDE 中正常工作。然后我从我的 IDE 构建了我的第一个 jar 并从批处理文件中运行它并且它可以工作,但不像预期的那样。

If the memory-file exists it is reed at program startup without problem.如果内存文件存在,它会在程序启动时毫无问题地读取。

But when I try to change program parameters or start program without memory-file and it is supposed to overwrite the memory-file with updated class object alt.但是当我尝试更改程序参数或启动没有内存文件的程序时,它应该用更新的 class object alt 覆盖内存文件。 create a new, it returns "FileNotFoundException".创建一个新的,它返回“FileNotFoundException”。

Here is my amateur code, I created a class with the purpose of writing/reading a "SaveClass" object to/from a text file:这是我的业余代码,我创建了一个 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;
    }
}

I have searched for information about how to approach my problem, but not found much I understand.我已经搜索了有关如何解决我的问题的信息,但没有找到我所理解的。 I tested to print the canWrite() on my save file while running the.jar program and it returned true.我测试了在运行 .jar 程序时在我的保存文件上打印 canWrite(),它返回了 true。

The best way to find out what's really happening is with these steps:找出真正发生的事情的最佳方法是执行以下步骤:

  • Replace all usage of java.io.File with the newer Path class.将 java.io.File 的所有用法替换为较新的路径class。
  • Replace all usage of FileOutputStream with Files.newOutputStream .将 FileOutputStream 的所有用法替换为Files.newOutputStream
  • Make sure every single catch block prints a stack trace.确保每个单独的catch块都打印堆栈跟踪。

java.io.File is a very old class. It has a number of design flaws, simply because API design wasn't as well understood in 1995. java.io.File 是一个非常古老的 class。它有许多设计缺陷,仅仅是因为 API 的设计在 1995 年还没有被很好地理解。

But the java.nio.file package is more modern and corrects all of those problems.但是java.nio.file package 更现代并且纠正了所有这些问题。 It also has much more detailed and informative exceptions.它还具有更详细和信息丰富的例外情况。

Using that package looks very similar:使用 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();
    }
}

This will print an exception that explains exactly why the file cannot be written.这将打印一个异常,准确解释无法写入文件的原因。

(Notice that I am using a try-with-resources statement—that is, the ObjectOutputStream creation is inside parentheses right after try —which will automatically close the ObjectOutputStream, which in turn will close the underlying OutputStream.) (请注意,我使用的是try-with-resources语句——也就是说,ObjectOutputStream 的创建是在try之后的括号内——这将自动关闭 ObjectOutputStream,后者又将关闭底层的 OutputStream。)

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

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