I am trying to save a file in Java in this way:
PrintWriter output = null;
int x=5, y = 6;
try {
saveFile = new FileOutputStream("myFile.txt");
save = new ObjectOutputStream(saveFile);
save.writeObject(x + y);
save.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
and it is working but how can I save an object from another class because it is giving me an error when I try to do it.
Like the others say, plenty of things that might be problematic … However serialization is one way to store a object in a file and read it back to a object from a file.
http://www.java-samples.com/showtutorial.php?tutorialid=398