-
writing to files
i have two vectors and when i close my project i would like all of the contents of the vectors to be written to a file(s) and then when the program is run all of the contents can be written back into the vectors from the file(s)
i can't seem to find anything in the API that might do this, so does anyone have any ideas, i am pretty sure that it can be done though :)
-
What you are referring to is called Serialization. Most of Java's base classes implement the interface Serializable, which is just a tagging interface that states that the class can be serialized.
Anyway, to serialize an object, you use the java.io.ObjectInputStream, and java.io.ObjectInputStream classes. You use the writeObject() method to serialize, and the readObject() method to deserialize. When using readObject() the return is an Object, so you'll have to cast the return back to a Vector, or whatever object you use.
ObjectInputStream
ObjectOutputStream
:)
-
cheeers i will give them a go :)
-
Have a look at 8.Files in Mark's notes. It has information about wrting/reading from files and has code which you can copy and paste, you'll just need to change a few names as necessary. I think there's a section about it in Horton as well
-
i did look, but it only confused me more, but i have got it working now :)
-
hmmmmmmmmmmmmmmm I wonder how.....
:p
-
-
Just in case anyone does want some code for file output:
Code:
import java.io.*;
public class FileSavingDemo implements Serializable
{
File myFile = new File("sd.rnd");
SerializableDemo sd;
public void init()
{
if (myFile.exists()) //the file has been created, therefore can be used
{
try
{
FileInputStream in = new FileInputStream(myFile);
ObjectInputStream p;
try
{
p = new ObjectInputStream(in);
Object s;
try
{
s = p.readObject();
}
catch(ClassNotFoundException e)
{
s = "";
}
}
catch(StreamCorruptedException e)
{
sd = new SerializableDemo(new java.util.Date().toString());
System.out.println("Corrupted File, created to be written: " + sd.getText());
}
if (s instanceof SerializableDemo)
{
sd = (SerializableDemo)s;
System.out.println("Read from file: " + sd.getText());
}
else
{
sd = new SerializableDemo(new java.util.Date().toString());
}
}
catch(IOException e)
{
e.printStackTrace();
System.out.println("Error on IO, created to be written: " + sd.getText());
sd = new SerializableDemo(new java.util.Date().toString());
}
}
else
{
sd = new SerializableDemo(new java.util.Date().toString());
System.out.println("No file detected, created to be written: " + sd.getText());
}
}
public void save()
{
try
{
FileOutputStream out = new FileOutputStream(myFile);
ObjectOutputStream p = new ObjectOutputStream(out);
sd = new SerializableDemo(new java.util.Date().toString());
System.out.println("Written to file: " + sd.getText());
p.writeObject(sd);
p.flush();
out.close();
}
catch(IOException e){}
}
public class SerializableDemo implements Serializable
{
String text;
public SerializableDemo(String theText)
{
text = theText;
}
public void setText(String theText)
{
text = theText;
}
public String getText()
{
return text;
}
}
public static void main(String [] args)
{
FileSavingDemo fd = new FileSavingDemo();
fd.init();
fd.save();
}
}
If anyone needs any more help let me know............
:)
-