Merge pull request #16 from PM2-IT21bWIN-ruiz-mach-krea/FileIO
FileIO completed including Java Doc and logging
This commit is contained in:
		
						commit
						b2c23e158d
					
				| 
						 | 
					@ -11,14 +11,20 @@ import java.util.Arrays;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.logging.Logger;
 | 
					import java.util.logging.Logger;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Class in Charge of Reading and Writing files
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
public class FileIO {
 | 
					public class FileIO {
 | 
				
			||||||
    private File mainDir;
 | 
					    private File mainDir;
 | 
				
			||||||
    private File saves;
 | 
					    private File saves;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private static final Logger logger = Logger.getLogger(FileIO.class.getName());
 | 
					    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) {
 | 
					    public FileIO(String saveLocation) {
 | 
				
			||||||
        this.mainDir = new File(saveLocation);
 | 
					        this.mainDir = new File(saveLocation);
 | 
				
			||||||
        if (!mainDir.exists()) {
 | 
					        if (!mainDir.exists()) {
 | 
				
			||||||
| 
						 | 
					@ -37,6 +43,11 @@ 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 ObservableList<TournamentFile> getList() {
 | 
					    public ObservableList<TournamentFile> getList() {
 | 
				
			||||||
        logger.fine("Creating a List out of all Files in the save directory and returning it");
 | 
					        logger.fine("Creating a List out of all Files in the save directory and returning it");
 | 
				
			||||||
        ObservableList<TournamentFile> tournamentFiles = FXCollections.observableArrayList();
 | 
					        ObservableList<TournamentFile> tournamentFiles = FXCollections.observableArrayList();
 | 
				
			||||||
| 
						 | 
					@ -56,12 +67,15 @@ public class FileIO {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * @param tournamentFile
 | 
					     * Loads and returns a tournament from a given File which contains the serialiazed tournament.
 | 
				
			||||||
     * @return
 | 
					     *
 | 
				
			||||||
     * @throws ClassNotFoundException
 | 
					     * @param tournamentFile The tournament file where the data should be read from.
 | 
				
			||||||
     * @throws IOException            File not found or not readable.
 | 
					     * @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) {
 | 
					        if (tournamentFile == null) {
 | 
				
			||||||
            logger.warning("Given tournament file is empty");
 | 
					            logger.warning("Given tournament file is empty");
 | 
				
			||||||
            throw new IllegalArgumentException("Tournament File is null");
 | 
					            throw new IllegalArgumentException("Tournament File is null");
 | 
				
			||||||
| 
						 | 
					@ -80,10 +94,10 @@ public class FileIO {
 | 
				
			||||||
            throw e;
 | 
					            throw e;
 | 
				
			||||||
        } catch (IOException e) {
 | 
					        } catch (IOException e) {
 | 
				
			||||||
            logger.severe("Failed to read File" + tournamentFile.getName());
 | 
					            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) {
 | 
					        } catch (ClassNotFoundException e) {
 | 
				
			||||||
            logger.severe("No definition for the class with the specified name could be found");
 | 
					            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 {
 | 
					        } finally {
 | 
				
			||||||
            if (in != null) {
 | 
					            if (in != null) {
 | 
				
			||||||
                try {
 | 
					                try {
 | 
				
			||||||
| 
						 | 
					@ -91,37 +105,49 @@ public class FileIO {
 | 
				
			||||||
                    in.close();
 | 
					                    in.close();
 | 
				
			||||||
                } catch (IOException e) {
 | 
					                } catch (IOException e) {
 | 
				
			||||||
                    logger.severe("Failed to close input stream");
 | 
					                    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;
 | 
					        return tournament;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void saveTournament(Tournament tournament) throws IOException {
 | 
					    /**
 | 
				
			||||||
 | 
					     * 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) {
 | 
					        if (tournament == null) {
 | 
				
			||||||
            logger.warning("Given tournament file is empty");
 | 
					            logger.warning("Given tournament file is empty");
 | 
				
			||||||
            throw new IllegalArgumentException("Null tournament received");
 | 
					            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");
 | 
					        File newSave = new File(saves, tournament.getName() + ".txt");
 | 
				
			||||||
        ObjectOutputStream out = null;
 | 
					        ObjectOutputStream out = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            boolean newFile = newSave.createNewFile();
 | 
					            logger.fine("creates or overwrites the new save FILE");
 | 
				
			||||||
 | 
					            newSave.createNewFile();
 | 
				
			||||||
            out = new ObjectOutputStream(new FileOutputStream(newSave));
 | 
					            out = new ObjectOutputStream(new FileOutputStream(newSave));
 | 
				
			||||||
            out.writeObject(tournament);
 | 
					            out.writeObject(tournament);
 | 
				
			||||||
            System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath());
 | 
					            System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        } catch (FileNotFoundException e) {
 | 
					        } catch (FileNotFoundException e) {
 | 
				
			||||||
            throw new RuntimeException(e);
 | 
					            logger.severe("Could not find tournament File");
 | 
				
			||||||
 | 
					            throw e;
 | 
				
			||||||
        } catch (IOException 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 {
 | 
					        } finally {
 | 
				
			||||||
            if (out != null) {
 | 
					            if (out != null) {
 | 
				
			||||||
                try {
 | 
					                try {
 | 
				
			||||||
                    out.close();
 | 
					                    out.close();
 | 
				
			||||||
                } catch (IOException e) {
 | 
					                } catch (IOException e) {
 | 
				
			||||||
                    throw new RuntimeException(e);
 | 
					                    logger.severe("Failed to close output stream");
 | 
				
			||||||
 | 
					                    throw new IOException("Error while closing output stream", e);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -129,6 +155,24 @@ public class FileIO {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 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");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
    public class TournamentFile extends File{
 | 
					    public class TournamentFile extends File{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public TournamentFile(URI uri) {
 | 
					        public TournamentFile(URI uri) {
 | 
				
			||||||
| 
						 | 
					@ -141,8 +185,4 @@ public class FileIO {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void deleteTournament(File tournamentFile) throws IOException {
 | 
					 | 
				
			||||||
        throw new UnsupportedOperationException("Method deleteTournament not implemented yet.");
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue