diff --git a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java index 801b6e0..22c7cfe 100644 --- a/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java +++ b/app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/FileIO.java @@ -6,14 +6,20 @@ import java.util.Arrays; import java.util.List; import java.util.logging.Logger; - +/** + * Class in Charge of Reading and Writing files + */ public class FileIO { private File mainDir; private File saves; private static final Logger logger = Logger.getLogger(FileIO.class.getName()); - + /** + * Constructor initiates the Directory and creates a save folder if not already existing. + * + * @param saveLocation the directory in which the save Files will be saved + */ public FileIO(String saveLocation) { this.mainDir = new File(saveLocation); if (!mainDir.exists()) { @@ -32,18 +38,26 @@ public class FileIO { } } + /** + * Returns a list with all save Files that are located inside the save folder. + * + * @return List with all containing save Files + */ public List getList() { logger.fine("Creating a List out of all Files in the save directory and returning it"); return Arrays.asList(saves.listFiles()); } /** - * @param tournamentFile - * @return - * @throws ClassNotFoundException - * @throws IOException File not found or not readable. + * Loads and returns a tournament from a given File which contains the serialiazed tournament. + * + * @param tournamentFile The tournament file where the data should be read from. + * @return Tournament that is returned when succefully being read from the file + * @throws ClassNotFoundException No definition for the class with the specified name could be found + * @throws IOException File not readable + * @throws FileNotFoundException File not found */ - public Tournament loadTournament(File tournamentFile) throws IOException, ClassNotFoundException { + public Tournament loadTournament(File tournamentFile) throws IOException, ClassNotFoundException, FileNotFoundException { if (tournamentFile == null) { logger.warning("Given tournament file is empty"); throw new IllegalArgumentException("Tournament File is null"); @@ -62,10 +76,10 @@ public class FileIO { throw e; } catch (IOException e) { logger.severe("Failed to read File" + tournamentFile.getName()); - throw new IOException("Error while reading File",e); + throw new IOException("Error while reading File", e); } catch (ClassNotFoundException e) { logger.severe("No definition for the class with the specified name could be found"); - throw new ClassNotFoundException("No definition for the class with the specified name could be found",e); + throw new ClassNotFoundException("No definition for the class with the specified name could be found", e); } finally { if (in != null) { try { @@ -73,37 +87,49 @@ public class FileIO { in.close(); } catch (IOException e) { logger.severe("Failed to close input stream"); - throw new IOException("Error while closing input stream",e); + throw new IOException("Error while closing input stream", e); } } } return tournament; } - public void saveTournament(Tournament tournament) { + /** + * Serializables and saves the receiving tournament file to a txt file. + * + * @param tournament the receiving tournament. + * @throws IOException File not readable + * @throws FileNotFoundException File not found + */ + public void saveTournament(Tournament tournament) throws IOException, FileNotFoundException { if (tournament == null) { logger.warning("Given tournament file is empty"); throw new IllegalArgumentException("Null tournament received"); } + logger.fine("Creates the Object for the path of the save file"); File newSave = new File(saves, tournament.getName() + ".txt"); ObjectOutputStream out = null; try { + logger.fine("creates or overwrites the new save FILE"); newSave.createNewFile(); out = new ObjectOutputStream(new FileOutputStream(newSave)); out.writeObject(tournament); System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath()); } catch (FileNotFoundException e) { - throw new RuntimeException(e); + logger.severe("Could not find tournament File"); + throw e; } catch (IOException e) { - throw new RuntimeException(e); + logger.severe("Failed to write File" + tournament.getName()); + throw new IOException("Error while writing File", e); } finally { if (out != null) { try { out.close(); } catch (IOException e) { - throw new RuntimeException(e); + logger.severe("Failed to close output stream"); + throw new IOException("Error while closing output stream", e); } } @@ -111,8 +137,23 @@ public class FileIO { } - public void deleteTournament(File tournamentFile) throws IOException { - throw new UnsupportedOperationException("Method deleteTournament not implemented yet."); + /** + * Deletes the requested File in the directory + * + * @param tournamentFile the file that should be deleted + * @throws FileNotFoundException File could not be deleted + * @throws IllegalArgumentException File is null + */ + public void deleteTournament(File tournamentFile) throws FileNotFoundException { + if (tournamentFile == null) { + throw new IllegalArgumentException("Receiving file is null"); + } + if (tournamentFile.delete()) { + logger.fine("deleted requested File"); + } else { + logger.warning("Failed to delete requested File"); + throw new FileNotFoundException("File deletion unsuccessful"); + } } }