Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class RankingJere {
- //Le pasamos el archivo como parametro para que pueda escribir en el y el puntaje de la partida actual;
- public static void setPuntaje (File scoreFile,String highScore ){
- FileWriter writeFile = null;
- BufferedWriter writer=null;
- try{
- writeFile = new FileWriter(scoreFile);
- writer = new BufferedWriter(writeFile);
- //writer.write(this.highScore);
- writer.write(highScore);
- }catch(Exception e){
- //errores
- }
- finally{
- try{
- if (writer!=null) {
- writer.close();
- }
- } catch(Exception e){
- //errosr
- }
- }
- }
- //
- //EL ENCARGADO DEL LEER EL ARCHIVO CREADO POR EL JUEGO
- public static String getPuntaje () throws FileNotFoundException
- {
- String ret = "";
- FileReader readFile = null ;
- BufferedReader reader =null;
- //formato = Jorge: 666
- try
- {
- readFile = new FileReader ("highscore.txt");
- reader = new BufferedReader (readFile);
- return reader.readLine();
- }
- catch (Exception e)
- {
- ret = " nadie : 0" ;
- }
- finally{
- try
- {
- if(reader!=null)
- {
- reader.close();
- }
- }
- catch (Exception e2)
- {
- e2.printStackTrace();
- }
- }
- return ret ;
- }
- //
- //EL TEST CONSISTE EN CAMBIAR LAS VARIABLES Y VER QUE EL ARCHIVO QUE SE GENERA SE VA SOBRE ESCRIBIENDO
- //PISANDO ASI EL PUNTAJE ANTERIOR
- //FALTA COMPARAR EL puntajeMain CON EL VALOR ENTERO DEL ARCHIVO ("highScore.txt")
- // SI putajeMain > VALOR_ENTERO_DENTRO_DEL_STRING
- public static void main(String[] args) throws FileNotFoundException {
- //variables
- String nombreJugador ="jorge";
- int puntajeMain = 1040 ;
- String highScoreMain = "";
- highScoreMain= nombreJugador + " : " + puntajeMain ;
- //crea archivo
- File scoreFile =new File ("highscore.txt");
- if (!scoreFile.exists())
- {
- try {
- scoreFile.createNewFile();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //fin de la creacion del archivo
- //variable que supone ya saque el valor dentro del String del archivo
- int x = 1000;
- if ( puntajeMain > x )
- {
- setPuntaje(scoreFile , highScoreMain);
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement