Merge pull request #16 from PM2-IT21bWIN-ruiz-mach-krea/FileIO

FileIO completed including Java Doc and logging
This commit is contained in:
Roman Schenk 2022-05-01 22:47:33 +02:00 committed by GitHub Enterprise
commit b2c23e158d
1 changed files with 59 additions and 19 deletions

View File

@ -11,14 +11,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()) {
@ -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() {
logger.fine("Creating a List out of all Files in the save directory and returning it");
ObservableList<TournamentFile> tournamentFiles = FXCollections.observableArrayList();
@ -56,12 +67,15 @@ public class FileIO {
}
/**
* @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");
@ -80,10 +94,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 {
@ -91,37 +105,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) 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) {
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 {
boolean newFile = newSave.createNewFile();
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);
}
}
@ -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 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.");
}
}