G
Gelöschtes Mitglied 5909
Gast
Hab mir ne klasse geschrieben die die statistiken von meinem kleinen game speichert, beim filepath hab ich aber so meine kleinen probleme:
Ich will keinen relativen pfadt (\bla.dat) sondern das betriebssystem soll ihn herrausfinden (aktuellen pfadt)
In dem pfadt möchte ich dann die datei speichern. allerdings bekomme ich den wert null zurückgeliefert.
Es handelt sich um Zeile 24, hab zum testen es mal manuell mit "\\" gemacht, aber ich will es ja OS unabhängig machen....
Ich will keinen relativen pfadt (\bla.dat) sondern das betriebssystem soll ihn herrausfinden (aktuellen pfadt)
In dem pfadt möchte ich dann die datei speichern. allerdings bekomme ich den wert null zurückgeliefert.
Es handelt sich um Zeile 24, hab zum testen es mal manuell mit "\\" gemacht, aber ich will es ja OS unabhängig machen....
Code:
package de.hska.faki.io;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InvalidClassException;
import java.io.StreamCorruptedException;
import java.io.OptionalDataException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import -----.Statistics;
/**
* An IOHandler which can save and load statistics.
* @author -----
*/
public class IOHandler implements Serializable {
//private static String fileSeperator = System.getProperty("file.seperator");
private static String fileSeperator = "\\";
private static File file = new File(System.getProperty("user.dir") +
fileSeperator + "statsistics.dat");
/**
* Creates a new IOHandler which can save and load statistics.
*/
public IOHandler() {
}
/**
* Saves statistics to a file, creates a new file if no file is found.
* @param statistics Statistics to save
* @return true if successfull, false if not
*/
public boolean saveStatistics(Statistics statistics) {
try {
file.createNewFile();
ObjectOutputStream output = new ObjectOutputStream (new FileOutputStream(file));
if (statistics != null) {
output.writeObject(statistics);
}
output.flush();
output.close();
return true;
}
catch (FileNotFoundException fnfe) {
System.err.println("Error: " + fnfe.getMessage());
}
catch (IOException ioe) {
System.err.println("Error: " + ioe.getMessage());
}
return false;
}
/**
* Loads statistics from a file, creates new statistics if no file was found.
* @return Statistics
*/
public Statistics loadStatistics() {
try {
//file.createNewFile();
ObjectInputStream input = new ObjectInputStream (new FileInputStream(file));
Statistics statistics = (Statistics) input.readObject();
input.close();
if (statistics != null) {
return statistics;
}
}
catch(FileNotFoundException fnfe) {
System.err.println("Error: " + fnfe.getMessage());
}
catch(ClassNotFoundException cnfe) {
System.err.println("Error: " + cnfe.getMessage());
}
catch(InvalidClassException ice) {
System.err.println("Error: " + ice.getMessage());
}
catch(StreamCorruptedException sce) {
System.err.println("Error: " + sce.getMessage());
}
catch(OptionalDataException ode) {
System.err.println("Error: " + ode.getMessage());
}
catch(IOException ioe) {
System.err.println("Error: " + ioe.getMessage());
}
return (new Statistics());
}
}