|
-
Mar 9th, 2002, 05:32 PM
#1
Thread Starter
Fanatic Member
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
Last edited by zmerlinz; Mar 26th, 2002 at 08:58 PM.
Some people have told me they don't think a fat penguin really embodies the grace of Linux, which just tells me they have never seen a angry penguin charging at them in excess of 100mph. They'd be a lot more careful about what they say if they had.
-- Linus Torvalds
[ Galahtech.com] | [ My Site] | [ Fishsponge] | [ UnixForum.co.uk]
-
Mar 9th, 2002, 05:47 PM
#2
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
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Mar 9th, 2002, 05:50 PM
#3
Thread Starter
Fanatic Member
cheeers i will give them a go
Some people have told me they don't think a fat penguin really embodies the grace of Linux, which just tells me they have never seen a angry penguin charging at them in excess of 100mph. They'd be a lot more careful about what they say if they had.
-- Linus Torvalds
[ Galahtech.com] | [ My Site] | [ Fishsponge] | [ UnixForum.co.uk]
-
Mar 10th, 2002, 05:52 PM
#4
Hyperactive Member
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
-
Mar 10th, 2002, 08:12 PM
#5
Thread Starter
Fanatic Member
i did look, but it only confused me more, but i have got it working now
Some people have told me they don't think a fat penguin really embodies the grace of Linux, which just tells me they have never seen a angry penguin charging at them in excess of 100mph. They'd be a lot more careful about what they say if they had.
-- Linus Torvalds
[ Galahtech.com] | [ My Site] | [ Fishsponge] | [ UnixForum.co.uk]
-
Mar 26th, 2002, 06:44 PM
#6
-
Mar 26th, 2002, 08:58 PM
#7
Thread Starter
Fanatic Member
erm yes i wonder
Some people have told me they don't think a fat penguin really embodies the grace of Linux, which just tells me they have never seen a angry penguin charging at them in excess of 100mph. They'd be a lot more careful about what they say if they had.
-- Linus Torvalds
[ Galahtech.com] | [ My Site] | [ Fishsponge] | [ UnixForum.co.uk]
-
Mar 28th, 2002, 03:19 PM
#8
Member
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............
-
Mar 29th, 2002, 08:02 AM
#9
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|