00001 package gui.InputOutput;
00002 import java.io.File;
00003 import java.io.FileInputStream;
00004 import java.io.FileNotFoundException;
00005 import java.io.FileOutputStream;
00006 import java.io.IOException;
00007 import java.io.ObjectInputStream;
00008 import java.io.ObjectOutputStream;
00009
00010 import commandline.debuggingCode.Debug;
00011
00012
00019 public class ObjectReaderWriter {
00020
00021 public static void write(Object o, String filename){
00022
00023 FileOutputStream fileOutputStream=null;
00024 try{
00025 fileOutputStream=new FileOutputStream(filename);
00026 }
00027 catch(FileNotFoundException fileNotFoundException){
00028 Debug.print("File "+filename +"not found");
00029 }
00030 ObjectOutputStream objectOutputStream=null;
00031 try{
00032 objectOutputStream =new ObjectOutputStream(fileOutputStream);
00033 objectOutputStream.writeObject(o);
00034 objectOutputStream.close();
00035 }catch(IOException exception){
00036 exception.printStackTrace();
00037 }
00038 Debug.print("object written to file");
00039 }
00040
00041
00042
00043 public static Object read(String filename) throws FileNotFoundException{
00044 FileInputStream fileInputStream=null;
00045 fileInputStream=new FileInputStream(filename);
00046
00047 ObjectInputStream inputStream=null;
00048 Object o=null;
00049 try{
00050 inputStream=new ObjectInputStream(fileInputStream);
00051 try{
00052 o=inputStream.readObject();
00053 }
00054 catch(ClassNotFoundException classNotFoundException){
00055 classNotFoundException.printStackTrace();
00056 o=null;
00057 }
00058 inputStream.close();
00059 }catch(IOException exception){
00060 System.out.print("I/O-exception");
00061 Debug.print("object NOT retrieved from file");
00062
00063 return null;
00064 }
00065 Debug.print("restoring configuration from file");
00066 return o;
00067 }
00068
00069 public static File createFile(String filename){
00070
00071 File file=new File(filename);
00072 try{
00073 if(file.createNewFile()){Debug.print("file "+ filename + "already existed");}
00074 }
00075 catch(IOException exception){
00076 exception.printStackTrace();
00077 }
00078 return file;
00079 }
00080
00081
00082 }