簡體   English   中英

使用 jar 文件讀取/寫入外部 .txt 文件

[英]Read/Write to an external .txt file using jar file

我是新手,這是我的第一篇文章。 我已經在 eclipse 上制作了一個完美運行的游戲應用程序。 它使用一些 .txt 文件作為分數和玩家選項。

問題是當我嘗試將它導出為可運行的 jar 文件時,這就是問題所在,它生成了一個 jar 文件,使我無法寫入我擁有的任何 .txt 文件。 我知道這一點,因為我嘗試了,不是數百個而是接近解決方案,其中一些允許我讀取文件,但我仍然無法在上面寫。 我意識到這是 jar 文件的正常功能,所以我的問題是:

如何在包含所有 txt 文件的同一目錄中為 jar 文件創建/創建外部文件夾? 以便它可以讀取和寫入這些文件,以及我應該在現有代碼中使用哪些方法?

我只展示了我如何讀/寫其中一個文件,但對於其他每個文件都是一樣的,我還展示了對其他過去解決方案的一些評論:

    private final String cubesScore =  "resF\\score.txt";
    //private final String cubesScore =  "/score.txt";
    //private final String cubesScore =  "//resF//score.txt";

    try{
        /*
        try{
            File file = new File(cubesScore);
            //FileReader reader = new FileReader(new File(new File("."), "score.txt"));

            if(file.createNewFile()){
                System.out.println("File created successfully!");
            } else{
                System.out.println("File already exists.");
            }

        } catch(IOException e){
            System.out.println("An error occurred on score.txt!!");
        }
        */
        Scanner scanner = new Scanner(new File(cubesScore));

        //InputStream source = this.getClass().getResourceAsStream(cubesScore);
        //Scanner scanner = new Scanner(source);

        /*
        InputStream inputStream = this.getClass().getResourceAsStream(cubesScore);
        InputStreamReader inputReader = new InputStreamReader(inputStream);
        BufferedReader reader = new BufferedReader(inputReader);
        */
        int value;
        int i = 0;
        while(scanner.hasNext()){
            value = scanner.nextInt();
            if(value >= 0){
                mostCubesDestroyed[i] = value;
                System.out.println(value);
            }
            else 
                System.out.println("File corrupted");
            ++i;
        }
        scanner.close();
    } catch (NullPointerException | FileNotFoundException e) {
        System.out.println("File Not Found/Null");
    }

寫:

        try {
             PrintWriter out = new PrintWriter(cubesScore);
             //OutputStream out = this.getClass().getResourceAsStream(cubesScore);
             //out = new PrintWriter(out);
             //BufferedWriter out = new BufferedWriter(new FileWriter(cubesScore));
             //out.write(mostCubesDestroyed[0]);
             //out.newLine();
             out.println(mostCubesDestroyed[0]);
             System.out.println(mostCubesDestroyed[0]+" Cubes GameMode 0");
             //out.write(mostCubesDestroyed[1]);
             //out.newLine();
             out.println(mostCubesDestroyed[1]);
             System.out.println(mostCubesDestroyed[1]+" Cubes GameMode 1");
             //out.write(mostCubesDestroyed[2]);
             //out.newLine();
             out.println(mostCubesDestroyed[2]);
             System.out.println(mostCubesDestroyed[2]+" Cubes GameMode 2");
             //out.write(mostCubesDestroyed[3]);
             //out.newLine();
             out.println(mostCubesDestroyed[3]);
             System.out.println(mostCubesDestroyed[3]+" Total Cubes Destroyed");
             out.close();
          } catch (IOException e) {
                 System.out.println("Error, try again!!");
      }

我意識到保留注釋代碼會使閱讀有點困難,但我仍然想向您展示一些我嘗試過的東西......

我還嘗試在應用程序第一次運行時創建文件,但沒有成功:

      try{
            File file = new File(cubesScore);
            //FileReader reader = new FileReader(new File(new File("."), "score.txt"));

            if(file.createNewFile()){
                System.out.println("File created successfully!");
            } else{
                System.out.println("File already exists.");
            }

        } catch(IOException e){
            System.out.println("An error occurred on score.txt!!");
        }

所以這就是我的問題,如果有人以前來過這里並且以某種方式設法找到解決方案,或者您只是知道如何產生所需的結果,即讀取/寫入 jar 外部的 .txt 文件(因為內部導致各種問題)然后請告訴我我應該怎么做,我看過一百多個帖子和視頻,但仍然找不到所需的解決方案。

編輯:這已在下面解決,結果我需要一個 . 在“/score.txt”上,一個 . 在所有文件中。

你試過這個嗎?:private final String CUBES_SCORE = "./score.txt"; 如果您想將它放在子目錄中,則還必須創建子目錄。 另外,看看這個: 如何創建一個文件並用Java寫入它?

我認為您的路徑 private final String cubesScore = "..\\resF\\score.txt"; 中存在一些問題。

希望能幫助到你 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM