Merge branch 'FileIO' into fxml

# Conflicts:
#	app/src/main/java/ch/zhaw/projekt2/turnierverwaltung/App.java
This commit is contained in:
schrom01
2022-04-30 16:10:19 +02:00
5 changed files with 243 additions and 1 deletions
@@ -0,0 +1,113 @@
package ch.zhaw.projekt2.turnierverwaltung;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;
public class FileIO {
private File mainDir;
private File saves;
private static final Logger logger = Logger.getLogger(FileIO.class.getName());
public FileIO(String saveLocation) {
this.mainDir = new File(saveLocation);
if (!mainDir.exists()) {
logger.fine("Creating main directory in given path" + saveLocation);
mainDir.mkdir();
} else {
logger.finer("main directory already exists");
}
saves = new File(mainDir, "saves");
if (!saves.exists()) {
saves.mkdir();
logger.fine("Creating save directory");
} else {
logger.finer("save directory already exists");
}
}
public List<File> 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.
*/
public Tournament loadTournament(File tournamentFile) throws IOException, ClassNotFoundException {
if (tournamentFile == null) {
logger.warning("Given tournament file is empty");
throw new IllegalArgumentException("Tournament File is null");
}
Tournament tournament;
logger.finer("Starting up Input Stream to read File");
ObjectInputStream in = null;
try {
logger.fine("Setting up input file and reading it");
FileInputStream fileInputStream = new FileInputStream(tournamentFile);
in = new ObjectInputStream(fileInputStream);
logger.finer("Starting to read tournament File");
tournament = (Tournament) in.readObject();
} catch (FileNotFoundException e) {
logger.severe("Could not find tournament File");
throw e;
} catch (IOException e) {
logger.severe("Failed to read File" + tournamentFile.getName());
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);
} finally {
if (in != null) {
try {
logger.finer("Trying to close input stream");
in.close();
} catch (IOException e) {
logger.severe("Failed to close input stream");
throw new IOException("Error while closing input stream",e);
}
}
}
return tournament;
}
public void saveTournament(Tournament tournament) {
if (tournament == null) {
logger.warning("Given tournament file is empty");
throw new IllegalArgumentException("Null tournament received");
}
File newSave = new File(saves, tournament.getName() + ".txt");
ObjectOutputStream out = null;
try {
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);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
@@ -1,6 +1,8 @@
package ch.zhaw.projekt2.turnierverwaltung;
public class Tournament {
import java.io.Serializable;
public class Tournament implements Serializable {
private String name;
public Tournament(String name){