2016-11-26 1 views
-1

J'ai un petit problème en Java. J'ai créé un nouveau fichier et que le fichier est dans la classe appelée mainPlayerVariables et sa variable est public static File savePlayerCoins = new File("c:\\Games\\Coin_Clicker\\saves\\pc.txt"); Mais dans le script mainGame où mon JFrame est i ajouté un JButton et cette fonction onClick sur elle:Erreur Java avec la création d'un nouveau fichier

mainPlayerVariables.playerCoins++; 
lblCoins.setText("x" + mainPlayerVariables.playerCoins); 

try { 
       BufferedWriter bwSaveCoins = new BufferedWriter(new FileWriter(mainPlayerVariables.savePlayerCoins)); 
       bwSaveCoins.write(mainPlayerVariables.playerCoins); 
       bwSaveCoins.close(); 

       mainPlayerVariables.savePlayerCoins.getParentFile().mkdirs(); 
       mainPlayerVariables.savePlayerCoins.createNewFile(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

Maintenant, je Je ne sais pas pourquoi Java affiche une erreur dans bufferedWriter et crée un nouveau code de fichier.

S'il vous plaît aidez si vous savez. Je vous remercie. Voici l'erreur:

java.io.FileNotFoundException: c:\Games\Coin_Clicker\saves\pc.txt (The system cannot find the path specified) 
at java.io.FileOutputStream.open0(Native Method) 
at java.io.FileOutputStream.open(Unknown Source) 
at java.io.FileOutputStream.<init>(Unknown Source) 
at java.io.FileOutputStream.<init>(Unknown Source) 
at java.io.FileWriter.<init>(Unknown Source) 
at mainGame.mainGamePlay$2.mouseReleased(mainGamePlay.java:82) 
at java.awt.Component.processMouseEvent(Unknown Source) 
at javax.swing.JComponent.processMouseEvent(Unknown Source) 
at java.awt.Component.processEvent(Unknown Source) 
at java.awt.Container.processEvent(Unknown Source) 
at java.awt.Component.dispatchEventImpl(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
at java.awt.Container.dispatchEventImpl(Unknown Source) 
at java.awt.Window.dispatchEventImpl(Unknown Source) 
at java.awt.Component.dispatchEvent(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$500(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.awt.EventQueue$4.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 

Si je fait une erreur dans l'option de fichier, s'il vous plaît me aider à le corriger. :)

+0

Il dit clairement FileNotFoundException droit? "Le système ne peut pas trouver le chemin spécifié" – developer

+0

oui, mais pourquoi. J'ai le code getParentFile(). Mkdirs(); –

+0

Votre code n'a aucun sens. Vous devriez appeler 'mkdirs()' * first * ', * puis *' new FileWriter() ', et écrire et fermer, et ne pas appeler' createNewFile() '* du tout. * – EJP

Répondre

1

Um, les gars, j'ai trouvé pourquoi, pas besoin de répondre. :) au fait Merci à tous. J'ai placé le code mainPlayerVariables.savePlayerCoins.getParentFile().mkdirs(); au mauvais endroit

c'est la correction:

mainPlayerVariables.savePlayerCoins.getParentFile().mkdirs(); 
try { 
      BufferedWriter bwSaveCoins = new BufferedWriter(new FileWriter(mainPlayerVariables.savePlayerCoins)); 
      bwSaveCoins.write(mainPlayerVariables.playerCoins); 
      bwSaveCoins.close(); 

      mainPlayerVariables.savePlayerCoins.createNewFile(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
+0

Juste pour que vous le sachiez, votre appel 'createNewFile()' ne fera rien car le fichier existe déjà à ce moment-là. – qxz

+0

Je le sais, merci. :) –

+0

Donc vous devriez l'enlever. – EJP