Results 1 to 9 of 9

Thread: writing to files

  1. #1

    Thread Starter
    Fanatic Member zmerlinz's Avatar
    Join Date
    May 2000
    Location
    in a world where the sun always shines on the bloody tv!!
    Posts
    604

    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]

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  3. #3

    Thread Starter
    Fanatic Member zmerlinz's Avatar
    Join Date
    May 2000
    Location
    in a world where the sun always shines on the bloody tv!!
    Posts
    604
    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]

  4. #4
    Hyperactive Member Pix's Avatar
    Join Date
    Feb 2001
    Location
    I'm not telling you (or them)
    Posts
    282
    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

  5. #5

    Thread Starter
    Fanatic Member zmerlinz's Avatar
    Join Date
    May 2000
    Location
    in a world where the sun always shines on the bloody tv!!
    Posts
    604
    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]

  6. #6
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33

    Talking

    hmmmmmmmmmmmmmmm I wonder how.....



    Infinity isn't large it's just incomprehensible

  7. #7

    Thread Starter
    Fanatic Member zmerlinz's Avatar
    Join Date
    May 2000
    Location
    in a world where the sun always shines on the bloody tv!!
    Posts
    604
    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]

  8. #8
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33
    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............



    Infinity isn't large it's just incomprehensible

  9. #9
    Hyperactive Member Pix's Avatar
    Join Date
    Feb 2001
    Location
    I'm not telling you (or them)
    Posts
    282

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width