title: Java - Writing to txt in a JAR file
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5052311/how-can-a-java-program-use-files-inside-the-jar-for-read-and-write">How can a Java program use files inside the .jar for read and write?</a>  </p>
  </blockquote>
  
  
  
  <p>How do I write to a .txt file from a <a href="http://en.wikipedia.org/wiki/JAR_%28file_format%29" rel="nofollow noreferrer">JAR</a> java compiled project?</p>
  
  <p>When I run my projects, it doesn't give errors, but it just doesn't write to the .txt inside JAR file.</p>
  
  <p>I make the JAR file using:</p>
  
  <pre><code>netbeans clean /build tool
  </code></pre>
  
  <h3>Code:</h3>
  
  <pre><code>public class FileIO   {
      private File file;
      private Scanner filescScanner, lineScanner;
      private PrintWriter fileWriter;
      private String[][] data;
  
      public FileIO () {
          data = new String[100][2];
      }
  
      public String[][] getLineScores(){
          return this.loadHighscores(this.getClass().getResourceAsStream("LineHighscores.txt"));
      }
  
      public String[][] getTimeScores(){
          return this.loadHighscores(this.getClass().getResourceAsStream("TimeHighscores.txt"));
      }
  
      public void setLineScores( String name,String lines ){
          boolean found= false;
          data = this.getLineScores();
          for(int i = 0; i&lt;data.length &amp;&amp; !found ; i++){
              if(data[i][0] == null || "Niemand".equals(data[i][0])){
                  data[i][0]=name;
                  data[i][1]=lines;
                  found=true;
              }
          }
          this.saveHighscores(this.getClass().getResource("LineHighscores.txt"),data);
      }
  
      public void setTimeScores(String time, String name){
          boolean found= false;
          data = this.getLineScores();
          for(int i = 0; i&lt;data.length &amp;&amp; !found ; i++){
              if(data[i][0] == null || "Niemand".equals(data[i][0])){
                  data[i][0]=name;
                  data[i][1]=time;
                  found=true;
              }
  
          }
          this.saveHighscores(this.getClass().getResource("TimeHighscores.txt"),data);
      }
  
      private String[][] loadHighscores( InputStream resourceStream){
          int x=0;
          String test = "";
          try{
              filescScanner = new Scanner(resourceStream);
          }
          catch(Exception ioe){
              System.err.println(ioe);
          }
  
          if (filescScanner.hasNext()){
              while(filescScanner.hasNextLine()&amp;&amp; x&lt;100) {
                  lineScanner = new Scanner(filescScanner.nextLine());
                  lineScanner.useDelimiter("-/-");
  
                  data[x][0]=lineScanner.next();//name
                  data[x][1]=lineScanner.next();//data
                  x++;
              }
              lineScanner.close();
              filescScanner.close();
          }
          else{
              data[0][0] = "Niemand";
              data[0][1] = "0";
          }
          return data;
      }
  
      private void saveHighscores( URL resourceStream, String[][] data){
          int x=0;
          try {
              file = new File(resourceStream.toURI());
          } catch (URISyntaxException ex) {
              Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
          }
  
          try {
              fileWriter = new PrintWriter(file);
          } catch (FileNotFoundException ex) {
              Logger.getLogger(FileIO.class.getName()).log(Level.SEVERE, null, ex);
          }
          if(data.length&gt;x){
              while(data.length&gt;x &amp;&amp; data[x][0] != null ){
                  fileWriter.println(data[x][0]+"-/-"+data[x][1]);
                  x++;
              }
              fileWriter.close();
          }
      }
  
      public static void main(String[] args){
          FileIO file = new FileIO();
          file.setLineScores("55555555", "KoenKevin");
      }
  }
  </code></pre>