-1

I am trying to put/save highscore in a file : highscore.txt

But it doesn't work for me. Anybody see why?

Code:

public void miseAJour(IModele modele) {

  if (modele.isFin()) {

    if (modele.aGagner()) {
      Writer writer = null;

      try {
        writer = new BufferedWriter(new OutputStreamWriter(
            new FileOutputStream("/data/highscore.txt"), "utf-8"));
        writer.write(modele.time());
      } catch (IOException ex) {
        // report
      } finally {
        try { writer.close(); } catch (Exception ex) { //ignore }
      }
    }
  }
}
Kenster
  • 23,465
  • 21
  • 80
  • 106

1 Answers1

0
  • Firstly, you command a closing bracket
  • Second, try adding .. before your path name.
  • Also add an catch or a null check for the writer and/or model.time()

    public void miseAJour(IModele modele) {
    
       if (modele.isFin()) {
    
         if (modele.aGagner()) {
           Writer writer = null;
    
           try {
             writer = new BufferedWriter(new OutputStreamWriter(
                 new FileOutputStream("../data/highscore.txt"), "utf-8"));
             writer.write(modele.time());
           } catch (Exception ex) {
             System.out.println("Error: " + ex.getMessage());
           } finally {
             try { writer.close(); }
             catch (Exception ex) { 
                System.out.println("Error: " + ex.getMessage());
             }
           }
         }
       }
     }
    
moffeltje
  • 4,521
  • 4
  • 33
  • 57