created FileIOTest and changed Exception Handling of File IO
This commit is contained in:
@@ -16,19 +16,5 @@ public class App {
|
||||
System.out.println(new App().getGreeting());
|
||||
|
||||
FileIO io = new FileIO(System.getProperty("user.dir") + File.separator + "tournierverwaltung_angrynerds");
|
||||
Tournament tournament= new Tournament("helloo");
|
||||
io.saveTournament(tournament);
|
||||
|
||||
|
||||
List<File> tournaments = io.getList();
|
||||
|
||||
for (File name: tournaments) {
|
||||
|
||||
System.out.println(name.getName());
|
||||
}
|
||||
|
||||
Tournament retrieved = io.loadTournament(tournaments.get(0));
|
||||
System.out.println("data retreived:");
|
||||
System.out.println(retrieved.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
package ch.zhaw.projekt2.turnierverwaltung;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@@ -27,22 +22,24 @@ public class FileIO {
|
||||
return Arrays.asList(saves.listFiles());
|
||||
}
|
||||
|
||||
public Tournament loadTournament(File tournamentFile) {
|
||||
/**
|
||||
*
|
||||
* @param tournamentFile
|
||||
* @return
|
||||
* @throws ClassNotFoundException
|
||||
* @throws IOException File not found or not readable.
|
||||
*/
|
||||
public Tournament loadTournament(File tournamentFile) throws ClassNotFoundException, IOException {
|
||||
Tournament tournament;
|
||||
try {
|
||||
ObjectInputStream in = new ObjectInputStream(new FileInputStream(tournamentFile));
|
||||
tournament = (Tournament) in.readObject();
|
||||
in.close();
|
||||
System.out.println("Save File" + tournament.getName() + ".txt being saved to " + saves.getAbsolutePath());
|
||||
|
||||
} catch (ClassNotFoundException | IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
FileInputStream fileInputStream = new FileInputStream(tournamentFile);
|
||||
ObjectInputStream in = new ObjectInputStream(fileInputStream);
|
||||
tournament = (Tournament) in.readObject();
|
||||
in.close(); //TODO: Evtl Try/finally?
|
||||
System.out.println("Read File" + tournament.getName() + ".txt being read from " + saves.getAbsolutePath());
|
||||
return tournament;
|
||||
}
|
||||
|
||||
public void saveTournament(Tournament tournament) {
|
||||
public void saveTournament(Tournament tournament) throws IllegalArgumentException {
|
||||
File newSave = new File(saves, tournament.getName() + ".txt");
|
||||
try {
|
||||
newSave.createNewFile();
|
||||
|
||||
Reference in New Issue
Block a user