FileIO completed including Java Doc and logging
This commit is contained in:
parent
672ad409ce
commit
eb6d68e84f
|
@ -6,14 +6,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()) {
|
||||||
|
@ -32,18 +38,26 @@ 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 List<File> getList() {
|
public List<File> 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");
|
||||||
return Arrays.asList(saves.listFiles());
|
return Arrays.asList(saves.listFiles());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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,30 +94,42 @@ public class FileIO {
|
||||||
return tournament;
|
return tournament;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveTournament(Tournament tournament) {
|
/**
|
||||||
|
* 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 {
|
||||||
|
logger.fine("creates or overwrites the new save FILE");
|
||||||
newSave.createNewFile();
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,8 +137,23 @@ public class FileIO {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteTournament(File tournamentFile) throws IOException {
|
/**
|
||||||
throw new UnsupportedOperationException("Method deleteTournament not implemented yet.");
|
* 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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue