Advertisement
LaCaraDeLaVerga

RankingJere

Sep 14th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 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. //Le pasamos el archivo  como parametro para que pueda  escribir en el  y el puntaje de la partida actual;
  11.         public static void setPuntaje (File scoreFile,String highScore ){
  12.                 FileWriter writeFile = null;
  13.                 BufferedWriter writer=null;
  14.                   try{
  15.                       writeFile = new FileWriter(scoreFile);
  16.                       writer = new BufferedWriter(writeFile);
  17.                       //writer.write(this.highScore);
  18.                       writer.write(highScore);
  19.                 }catch(Exception e){
  20.                   //errores
  21.               }
  22.                   finally{
  23.                       try{
  24.                           if (writer!=null) {
  25.                           writer.close();
  26.                           }
  27.                       }   catch(Exception e){
  28.                           //errosr
  29.                         }
  30.                   }
  31.         }
  32. //
  33. //EL ENCARGADO DEL LEER EL ARCHIVO CREADO POR EL JUEGO
  34.               public static String  getPuntaje () throws FileNotFoundException
  35.                 {
  36.                     String ret = "";
  37.                     FileReader readFile = null ;
  38.                     BufferedReader reader =null;
  39.                     //formato =  Jorge: 666
  40.                         try
  41.                         {
  42.                             readFile = new FileReader ("highscore.txt");
  43.                               reader = new BufferedReader (readFile);
  44.                             return reader.readLine();
  45.                         }
  46.                        
  47.                             catch (Exception e)
  48.                             {
  49.                             ret = " nadie :  0" ;
  50.                             }
  51.                         finally{
  52.  
  53.                             try
  54.                             {
  55.                                 if(reader!=null)
  56.                                 {
  57.                                     reader.close();
  58.                                 }
  59.                             }
  60.                                 catch (Exception e2)
  61.                                 {
  62.                                 e2.printStackTrace();
  63.                                 }
  64.                     }
  65.                     return ret ;
  66.                 }
  67.         //
  68.              
  69. //EL TEST CONSISTE EN CAMBIAR LAS VARIABLES Y VER QUE EL ARCHIVO QUE SE GENERA SE VA SOBRE ESCRIBIENDO
  70. //PISANDO ASI EL PUNTAJE ANTERIOR
  71.  
  72. //FALTA COMPARAR EL puntajeMain CON EL VALOR ENTERO DEL ARCHIVO ("highScore.txt")
  73. // SI putajeMain > VALOR_ENTERO_DENTRO_DEL_STRING
  74.              
  75.     public static void main(String[] args) throws FileNotFoundException {
  76.     //variables
  77.         String nombreJugador ="jorge"; 
  78.         int puntajeMain = 1040 ;
  79.          String highScoreMain = "";
  80.         highScoreMain= nombreJugador + " : " + puntajeMain ;
  81.        
  82.     //crea archivo
  83.         File scoreFile =new File ("highscore.txt");
  84.          if (!scoreFile.exists())
  85.          {
  86.              try {
  87.                  scoreFile.createNewFile();
  88.              } catch (IOException e) {
  89.                  // TODO Auto-generated catch block
  90.                  e.printStackTrace();
  91.                 }
  92.          }
  93.     //fin de la creacion del archivo
  94.          
  95.          //variable que supone ya saque el valor dentro del String del archivo
  96.          int x = 1000;
  97.          
  98.          if ( puntajeMain > x )
  99.          {
  100.              setPuntaje(scoreFile , highScoreMain);
  101.             System.out.println();  
  102.  
  103.          }
  104.     }  
  105.      
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement