Cant write an Object to a file

HI, I am trying to save an ArrayList as a file but I get a NotSerializableException but the ArrayList class is Serializable, at least it was on 1.4.I am using eclipse and have a jvm1.6 installed.
[209 byte] By [MelGohana] at [2007-11-26 16:09:19]
# 1
But is what's in the ArrayList serializable? What's it an ArrayList of? And are all the objects reachable from those objects serializable?
ejpa at 2007-7-8 22:31:37 > top of Java-index,Core,Core APIs...
# 2

hi dude,

i have got a solution for u .seems it OK.Just try out .

import java.util.*;

import java.io.*;

public class Main {

public static void main(String[] args)

{

Properties prop = new Properties();

OutputStream out;

BufferedOutputStream br;

FileOutputStream fout;

try{

// fout = new FileOutputStream("a.txt");

//br = new BufferedOutputStream(out);

out = new FileOutputStream("a.txt");

prop.setProperty("name1","prajwal");

prop.setProperty("age","23");

prop.store(out,"emp");

}

catch(Exception e)

{}

}

}

the data will be written to harddisk.Like wise u can retreive the data back using load() .

Prajjia at 2007-7-8 22:31:37 > top of Java-index,Core,Core APIs...
# 3

hi dude

I m back again

Here is the exact stuff u needed

import java.util.*;

import java.io.*;

public class Main {

public static void main(String[] args)

{

FileOutputStream fout;

ObjectOutputStream out ;

List list = new ArrayList();

list.add(new A());

list.add(new A());

list.add(new A());

try

{

fout = new FileOutputStream("a.txt");

out = new ObjectOutputStream(fout);

out.writeObject(list);

}

catch(Exception e)

{}

}

}

class A implements Serializable

{

public int a = 10;

}

Prajjia at 2007-7-8 22:31:37 > top of Java-index,Core,Core APIs...
# 4
Thanks al ot. I already had solved the problem, I didnt realized the Serializable interface on my objects, was obvious but I guess I was distracted.
MelGohana at 2007-7-8 22:31:37 > top of Java-index,Core,Core APIs...
# 5
You have to implement serializable interface if you wish to persist objects. Thats the thumb rule
Goofy_111a at 2007-7-8 22:31:37 > top of Java-index,Core,Core APIs...
# 6

HI, I am trying to save Vector object to a file which contains an image as one element.But i am not able to do that can anybody tell how to write the program.

i wrote the following program to do tha task but it's not working

can anybody tell what changes i have to make my program to work properly

my code as follows

import java.util.*;

import java.io.*;

import java.awt.*;

class ImageWrit implements Serializable

{

Image img;

public ImageWrit(){

img= Toolkit.getDefaultToolkit().getImage("a9.bmp");

}

}

class A{

public static void main(String s[])

{

FileOutputStream fout;

ObjectOutputStream out ;

Vector list = new Vector();

list.add(new ImageWrit());

try{

fout = new FileOutputStream("a.txt");

out = new ObjectOutputStream(fout);

out.writeObject(list);

System.out.println("object is saved");

}

catch(Exception e)

{

System.out.println("error");

}

}

}

chokkanathana at 2007-7-8 22:31:37 > top of Java-index,Core,Core APIs...