簡體   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