Hi,
I am having trouble with the following code, it doesn't save anything or load anything.
Can someone point me in the right direction.
Database Class
Test Classpackage Testing; import java.beans.XMLDecoder; import java.beans.XMLEncoder; import java.io.FileInputStream; import java.io.FileOutputStream; public class DataBase { /** * The linked list of Tests */ private Tests tests; /* * The name of the XML file that contains the tests data used to read the saved data */ private final String TESTSFILE="Tests.xml"; public DataBase(){ boolean readOK=true; tests=new Tests(); readOK=true; System.out.println("Opening"); readOK = true; try{ XMLDecoder decoder = new XMLDecoder(new FileInputStream(TESTSFILE)); tests =(Tests) decoder.readObject(); decoder.close(); }catch (Exception e){ System.out.println(e.toString()); System.out.println("FileNotFound"); readOK=false; } if(!readOK){ }else{ System.out.println("Tests Read In"); } } public boolean saveSystem(){ boolean savedOK=true; try{ XMLEncoder encoder = new XMLEncoder(new FileOutputStream(TESTSFILE)); encoder.writeObject(tests); encoder.close(); }catch (Exception e){ } return savedOK; } public boolean addTest(Test test){ tests.add(test); return true; } public Test[] getAllTests(){ Test[] list = tests.getAllTests(); return list; } }
Tests Classpackage Testing; public class Test { private String firstPara; private String lastPara; public void setFirstPara(String firstPara) { this.firstPara = firstPara; } public String getFirstPara() { return this.firstPara; } public void setLastPara(String lastPara) { this.lastPara = lastPara; } public String getLastPara() { return this.lastPara; } public String toString(){ String result = ""; result = result + firstPara + ", "; result = result + lastPara + ", "; return result; } }
Start Class - Used to run the systemimport java.util.LinkedList; public class Tests { public LinkedList<Test> tests= new LinkedList<Test>(); public Tests(){ } public void add(Test test){ tests.add(test); } public void delete(Test test){ tests.remove(test); } public Test[] getAllTests() { int size = tests.size(); Test[] tests = new Test[size]; if(size>0){ for(int i = 0; i< size;i++){ tests[i]=this.tests.get(i); } } return tests; } }
Everything seems to work except for the saving/loading.package Testing; public class Start { public static void main(String[] args){ Test test = new Test(); DataBase mySystem = new DataBase(); Test[] tests; test.setFirstPara("Test1"); test.setLastPara("Test2"); mySystem.addTest(test); tests = mySystem.getAllTests(); for(int i=0; i < tests.length-1;i++){ System.out.println(tests[i].toString()); } if(!mySystem.saveSystem()){ System.out.println("Failed to save"); }else{ System.out.println("Saved"); } } }
Thanks for your time.