LaCaraDeLaVerga

file testing beta cuatro

Sep 15th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8.  
  9. public class RankingJere {
  10.  
  11. //     
  12. //      //Si no Existe el archivo ("highscore.dat") lo crea en la carpeta del projecto (al lado del BIM SRC  y esas cosillas ;
  13. //      public static void crearArchivo()
  14. //      {  
  15. //          File scoreFile =new File ("highscore.dat");
  16. //           if (!scoreFile.exists())
  17. //           {
  18. //               try {
  19. //                   scoreFile.createNewFile();
  20. //               } catch (IOException e) {
  21. //                   // TODO Auto-generated catch block
  22. //                   e.printStackTrace();
  23. //               }
  24. //             
  25. //           }
  26. //      }
  27.         // hasta aqui genera un archivo pues si no  esta y si esta será sobreescrito por el escritor que es el siguiente Method <3 ;
  28.        
  29.        
  30.        
  31.        
  32.         //Le pasamos el archivo  como parametro para que pueda  escribir en el  y el puntaje de la partida actual;
  33.         public static void setPuntaje (File scoreFile,String highScore ){
  34.                 FileWriter writeFile = null;
  35.                 BufferedWriter writer=null;
  36.                   try{
  37.                       writeFile = new FileWriter(scoreFile);
  38.                       writer = new BufferedWriter(writeFile);
  39.                       //writer.write(this.highScore);
  40.                       writer.write(highScore);
  41.                 }catch(Exception e){
  42.                   //errores
  43.               }
  44.                   finally{
  45.                       try{
  46.                           if (writer!=null) {
  47.                           writer.close();
  48.                           }
  49.                       }   catch(Exception e){
  50.                           //errosr
  51.                         }
  52.                   }
  53.         }
  54. //
  55.        
  56.        
  57.         //EL ENCARGADO DEL LEER EL ARCHIVO CREADO POR EL JUEGO
  58.        
  59.               public static String  getPuntaje () throws FileNotFoundException
  60.                 {
  61.                     String ret = "";
  62.                     FileReader readFile = null ;
  63.                     BufferedReader reader =null;
  64.                     //formato =  Jorge: 666
  65.                         try {
  66.                  
  67.                             readFile = new FileReader ("highscore.txt");
  68.                               reader = new BufferedReader (readFile);
  69.                             return reader.readLine();
  70.                         } catch (Exception e) {
  71.                             ret = " nadie :  0" ;
  72.                         }
  73.                         finally{
  74.                             try {
  75.                                 if(reader!=null){
  76.                                     reader.close();
  77.                                 }
  78.                             } catch (Exception e2) {
  79.                                 e2.printStackTrace();
  80.                             }
  81.                     }
  82.                     return ret ;
  83.                 }
  84.            
  85.        
  86.         //
  87.     public static void main(String[] args) throws FileNotFoundException {
  88.         //variables
  89.         String nombreJugador ="jorge"; 
  90.         int puntajeMain = 100 ;
  91.          String highScoreMain = "";
  92.         highScoreMain= nombreJugador + " : " + puntajeMain ;
  93.        
  94.        
  95. //      System.out.println( highScoreMain);
  96.         //crear archivo
  97.         File scoreFile =new File ("highscore.txt");
  98.          if (!scoreFile.exists())
  99.          {
  100.              try {
  101.                  scoreFile.createNewFile();
  102.              } catch (IOException e) {
  103.                  // TODO Auto-generated catch block
  104.                  e.printStackTrace();
  105.              }
  106.          }
  107.         //
  108.         //crearArchivo();
  109.         setPuntaje(scoreFile , highScoreMain);
  110.         System.out.println();
  111.         String actual=getPuntaje();
  112. //      System.out.println(actual);
  113.        
  114.    
  115.        
  116.        
  117.     }  
  118.      
  119. }
Add Comment
Please, Sign In to add comment