2017-01-24 8 views
1

を使用して外部の.txtファイルに読み書きします。これは私の最初の投稿です。私は完璧に動作する日食にゲームのアプリケーションを作った。それはスコアとプレーヤーオプションのためにいくつかの.txtファイルを使用します。jarファイル

問題は、実行可能なjarファイルとしてエクスポートしようとしたときです。問題は、jarファイルを作成して、私が持っている.txtファイルに書き込むことができなくなります。私はこれを知っている。なぜなら、iveは何百もの人ではなかったが解決策は近づいてきており、その中にはファイルを読むことができたものの、それでも私はそれらに書き込むことができなかったからだ。

私の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"); 
    } 

書き込み:

イムは、ファイルのみの1が、他のすべてのファイルに対して、その同じことを書いて、イムはまた、他の過去のソリューションにコメントの一部を示す/私が読ん様子

 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!!"); 
     } 

私がコメントしたコードを保つ実現を読み、それが少し難しくなりますが、それでも私はいくつかのものを試してみましたアイブをお見せしたかった...

IVEは、ファイルを作成するためにモミを試してみました時間tアプリの実行が、無成功へ:誰もが前にここにい何とか解決策を見つけることができたか、あなたは、単に読むことです望ましい結果を生成する方法を知っていれば

 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!!"); 
     } 

はそう、私の問題のthats/(内部があらゆる種類の問題につながるため)外部の.txtファイルに書き込んでから、何をすべきか教えてください。何百回ものポストやビデオが見られ、まだ解決策が見つかっていません。

編集:これは以下のように解決されました。 "/score.txt"にあります。すべてのファイルに

+0

。クラスパスの.jarファイルは読み取り専用です。 (システムによっては、書き込みを許可するかもしれませんが、保証されていないので、読み取り専用として扱うべきです) 'System.getProperty(" user .home ")'。 – VGR

+0

あなたの助けを借りてTyが解決したと思ったら、私は思ったより簡単でした... –

答えて

1

これを試しましたか?: プライベート最終文字列CUBES_SCORE = "./score.txt"; サブディレクトリに入れたい場合は、サブディレクトリも作成する必要があります。私はあなたのパス 民間最終文字列cubesScore =「.. \ RESF \ score.txt」のいくつかの問題があると思いHow do I create a file and write to it in Java?

+0

Ty alot、私はそう多くの時間と闘争と最後には、シングルを信じていないと信じています。 "/score.txt"は欠落していたリンクです.-、すでにテストして作業しました。ファイルを作成しましたが、最初に読み込んだ情報はありませんでした。情報をすべて0行に書き込んでから読み込みました。働いた。今私は別の問題を抱えていますが、この問題とは関係がありません。まず最初に病気を解決しようとします。病気ではないにしても新しい投稿を作成します。私はポストエンドを作る方法を知らない。 –

0

;: また、このを見てみましょう

希望します。

+0

問題が解決した、私は質問を閉じる方法がわからない:/まだ返信のためにタイ –