Hello!
I am about to load details from a file and save it to a node. I am having an error regarding to a certain code. Here's the code::
boolean loadFromFile(String filename) throws Exception{
boolean b = false;
try{
FileInputStream myfile = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(myfile);
do{
start = (Node)(in.readObject()); /*This part is the error. Cannot cast a String into a Node */
System.out.println(start.toString());
b = true;
}while(b == true);
in.close();
}catch (EOFException e){
System.out.println("EOF");
b = false;
}catch(Exception e){
System.out.println(e.getMessage());
System.out.println("Cannot load file");
}
return b;
}
You might also need the code for saveToFile. (since this might cause the problem)
boolean saveToFile(String filename) throws Exception{
boolean b = false;
try{
FileOutputStream myfile = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(myfile);
System.out.println("Writing files...");
index = start;
do{
out.writeObject(index); //Or out.writeObject(index.toString()) though I do not think this will work.
index = index.next;
}while(index!=null);
out.close();
}catch(Exception e){
System.out.println(e.getMessage());
System.out.println("File not existing.");
b = false;
}
return b;
}
Any suggestions regarding to this matter? Thank you very much.