java.lang.Object
edu.ntnu.idatt1002.k01g08.fta.util.FileManager

public class FileManager extends Object
Contains static methods for reading and writing teams and tournaments to JSON files.
  • Constructor Details

    • FileManager

      public FileManager()
  • Method Details

    • loadJson

      public static javax.json.JsonStructure loadJson(File file) throws IOException
      Reads and returns a JSON structure from a file.
      Parameters:
      file - a file to read from
      Returns:
      a JSON structure read from the file
      Throws:
      IOException - if the file for some reason could not be opened for reading
      javax.json.JsonException - if a JSON object or array cannot be created due to incorrect representation
    • loadJsonArray

      public static javax.json.JsonArray loadJsonArray(File file) throws IOException
      Reads and returns a JSON array from a file.
      Parameters:
      file - a file to read from
      Returns:
      a JSON structure read from the file
      Throws:
      IOException - if the file for some reason could not be opened for reading
      javax.json.JsonException - if a JSON array cannot be created due to incorrect representation
    • loadJsonObject

      public static javax.json.JsonObject loadJsonObject(File file) throws IOException
      Reads and returns a JSON object from a file.
      Parameters:
      file - a file to read from
      Returns:
      a JSON structure read from the file
      Throws:
      IOException - if the file for some reason could not be opened for reading
      javax.json.JsonException - if a JSON object cannot be created due to incorrect representation
    • saveJson

      public static void saveJson(javax.json.JsonStructure json, File file) throws IOException
      Saves a JSON structure to a file.
      Parameters:
      json - A JSON structure to save to a file
      file - the file to save the JSON object to
      Throws:
      IOException - if file for some reason could not be saved
    • saveIfAbsent

      public static boolean saveIfAbsent(javax.json.JsonObject json, File file) throws IOException
      Saves a JSON structure to a file, if the file does not already exist. Returns true if successful.
      Parameters:
      json - A JSON structure to save to a file
      file - the file to save the JSON object to
      Returns:
      true if the file was created
      Throws:
      IOException - if file for some reason could not be saved
    • loadTeamRegister

      public static TeamRegister loadTeamRegister(File file) throws IOException
      Loads a team register from a file. Convenience method for parseTeamRegister(loadJsonArray(file)).
      Parameters:
      file - a file to load a team register from
      Returns:
      a team register parsed from the contents of the file
      Throws:
      IOException - if the file for some reason could not be opened for reading
    • saveTeamRegister

      public static void saveTeamRegister(TeamRegister teamRegister, File file) throws IOException
      Saves a team register to a file.
      Parameters:
      teamRegister - a team register to save
      file - the file to save to
      Throws:
      IOException - if the team register for some reason could not be saved
    • loadTournament

      public static Tournament loadTournament(File file, TeamRegister teamRegister) throws IOException
      Loads a tournament from a file, pulling teams from a team register. Convenience method for parseTournament(loadJsonObject(file), teamRegister).
      Parameters:
      file - a file to load a tournament from
      Returns:
      a tournament parsed from the contents of the file
      Throws:
      IOException - if the file for some reason could not be opened for reading
    • saveTournament

      public static void saveTournament(File file, Tournament tournament) throws IOException
      Saves a tournament to a file.
      Parameters:
      tournament - a tournament to save
      file - the file to save to
      Throws:
      IOException - if the tournament for some reason could not be saved