简体   繁体   中英

Write to a file doesn't work in java

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 }
      }
    }
  }
}
  • 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()); } } } } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM