Merge remote-tracking branch 'origin/FileIO' into FileIO
This commit is contained in:
commit
4909bd0d7c
|
@ -4,57 +4,109 @@ package ch.zhaw.projekt2.turnierverwaltung;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
|
||||||
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());
|
||||||
|
|
||||||
|
|
||||||
public FileIO(String saveLocation) {
|
public FileIO(String saveLocation) {
|
||||||
this.mainDir = new File(saveLocation);
|
this.mainDir = new File(saveLocation);
|
||||||
|
if (!mainDir.exists()) {
|
||||||
|
logger.fine("Creating main directory in given path" + saveLocation);
|
||||||
mainDir.mkdir();
|
mainDir.mkdir();
|
||||||
|
} else {
|
||||||
|
logger.finer("main directory already exists");
|
||||||
|
}
|
||||||
|
|
||||||
saves = new File(mainDir, "saves");
|
saves = new File(mainDir, "saves");
|
||||||
|
if (!saves.exists()) {
|
||||||
saves.mkdir();
|
saves.mkdir();
|
||||||
|
logger.fine("Creating save directory");
|
||||||
|
} else {
|
||||||
|
logger.finer("save directory already exists");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<File> getList() {
|
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());
|
return Arrays.asList(saves.listFiles());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param tournamentFile
|
* @param tournamentFile
|
||||||
* @return
|
* @return
|
||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
* @throws IOException File not found or not readable.
|
* @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;
|
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);
|
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();
|
tournament = (Tournament) in.readObject();
|
||||||
in.close(); //TODO: Evtl Try/finally?
|
} catch (FileNotFoundException e) {
|
||||||
System.out.println("Read File" + tournament.getName() + ".txt being read from " + saves.getAbsolutePath());
|
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;
|
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");
|
File newSave = new File(saves, tournament.getName() + ".txt");
|
||||||
|
ObjectOutputStream out = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
newSave.createNewFile();
|
newSave.createNewFile();
|
||||||
} catch (IOException e) {
|
out = new ObjectOutputStream(new FileOutputStream(newSave));
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(newSave));
|
|
||||||
out.writeObject(tournament);
|
out.writeObject(tournament);
|
||||||
out.close();
|
|
||||||
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) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
} catch (IOException 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.nio.file.Path;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@ -107,7 +108,7 @@ class FileIOTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void saveTournament() {
|
void saveTournament() throws IOException {
|
||||||
Tournament tournament = new Tournament("test1");
|
Tournament tournament = new Tournament("test1");
|
||||||
io.saveTournament(tournament);
|
io.saveTournament(tournament);
|
||||||
File file = new File(mainDir + "/saves/test1.txt");
|
File file = new File(mainDir + "/saves/test1.txt");
|
||||||
|
|
Loading…
Reference in New Issue