Hey everyone,
i'm currently working on a program that requires a number to be saved outside of the program in some manner and recalled later. Sounds confusing but perhaps this will help. The program i'm making is a game, and i want to be able to create something similar to a best players board, that ranks the players time and name. So starting off i set the fastest time to 10000 seconds, if you beat that time it prompts you to enter your name and on the game over screen it shows the fastest time, this works ideally when you play the game continuously. HOWEVER, when the game is closed then reopened to play again it resets the fastest time to 10000 seconds again, (as it should) i was just wondering how i might be able to save the name and fastest time of the player so that no matter how many times the programs is closed and reopened the player who set the fastest time is always remembered. Is it even possible?

I think you are probably looking for a class that can save and load
a text file or a file of any file-type, which you can later load into memory.

Here are some basic sample classes.

Note:

These two concept classes aren't executable standalone-classes.
These classes are public and requires implementation of the main class.

What you need to do is the import these two classes into your main class,
and make sure to take a look at the class structure as well as keywords
as it is created by me and contains keywords which is not known from the std library.
I believe I have made it simple enough for others to understand.

Keep in mind that you may have to adjust these classes in order to load
and save the files properly, as the classes aren't specified for your program,
as they are only made as basic samples.

  • IMPORTANT KEYWORDS: *

Save(); -Just Save.
Save(String data); -Save anything you type inside the brackets.
Load(); -Will load and print the content of the file.

# DEMONSTRATION #

   private SaveFile saveFile;
   private LoadFile loadFile;

   // in constructor
   public myClass()
   {
       saveFile = new SaveFile("C:/Users/Username/Desktop/This.txt);
       loadFile = new LoadFile("C:/Users/Username/Desktop/This.txt);

       saveFile.Save();  // Will only save the file.
       saveFile.Save(" User01 41578 points "); // will save data.
       loadFile().Load();  // Will print the data saved in file.
   }

Code-Snippet for Save:

import java.io.*;

public class SaveFile 
{
    Writer save = null;
    File file;

    public SaveFile(String filename)
    {
        file = new File(filename);
    }

    public void Save(String data)
    {
        try
        {
            save = new BufferedWriter(new FileWriter(file));
            save.write(data);
            save.close();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }

    public void Save()
    {
        try
        {
            save = new BufferedWriter(new FileWriter(file));
            save.close();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }  
    }
}

Code Snippet for Load:

import java.io.*;
import java.util.*;

public class LoadFile 
{
    Scanner sc;

    public LoadFile(String filename)
    {
        try
        {
            sc = new Scanner(new File(filename));
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
            System.err.println("Could not load file");
        }
    }

    public void Load()
    {
        while (sc.hasNext())
            System.out.println(sc.nextLine());
    }
}

Hope this is considered helpful.

Edited 4 Years Ago by 117: code_error

This article has been dead for over six months. Start a new discussion instead.