Fxml #8
|
@ -4,57 +4,109 @@ 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 ClassNotFoundException, IOException {
|
||||
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);
|
||||
ObjectInputStream in = new ObjectInputStream(fileInputStream);
|
||||
in = new ObjectInputStream(fileInputStream);
|
||||
logger.finer("Starting to read tournament File");
|
||||
tournament = (Tournament) in.readObject();
|
||||
in.close(); //TODO: Evtl Try/finally?
|
||||
System.out.println("Read File" + tournament.getName() + ".txt being read from " + saves.getAbsolutePath());
|
||||
} 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) throws IllegalArgumentException {
|
||||
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();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(newSave));
|
||||
out = new ObjectOutputStream(new FileOutputStream(newSave));
|
||||
out.writeObject(tournament);
|
||||
out.close();
|
||||
System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath());
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import java.nio.file.Files;
|
|||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
@ -107,7 +108,7 @@ class FileIOTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
void saveTournament() {
|
||||
void saveTournament() throws IOException {
|
||||
Tournament tournament = new Tournament("test1");
|
||||
io.saveTournament(tournament);
|
||||
File file = new File(mainDir + "\\saves\\test1.txt");
|
||||
|
|
Loading…
Reference in New Issue