rvaldivia
asked on
How to save a file with records.
I have defined a class in Java similar to a Delphi's record, now I need to save those "records" in a file on my hard disk. Someone here gave this code:
/***********************
//Write your array of Records:
MyRecordClass[] aRecs = new MyRecordClass[10];
for (int i = 0; i < 10; i++)
aRecs[i] = new MyRecordClass();
}
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:\\myfi le.obj"));
oos.writeObject(aRecs);
oos.close();
/***********************
But when I tried to do it I got the following error.
Exception occurred during event dispatching:
java.security.AccessContro lException : access denied (java.io.FilePermission e:\m
yfile.obj write)
at java.security.AccessContro lContext.c heckPermis sion(Acces sControlCo nte
xt.java:272)
at java.security.AccessContro ller.check Permission (AccessCon troller.ja va:
399)
at java.lang.SecurityManager. checkPermi ssion(Secu rityManage r.java:545 )
at java.lang.SecurityManager. checkWrite (SecurityM anager.jav a:978)
at java.io.FileOutputStream.< init>(File OutputStre am.java:96 )
at java.io.FileOutputStream.< init>(File OutputStre am.java:62 )
at Clientes.GrabaFile(Cliente s.java:296 )
at Clientes.actionPerformed(C lientes.ja va:334)
at java.awt.Button.processAct ionEvent(B utton.java :329)
at java.awt.Button.processEve nt(Button. java:302)
at java.awt.Component.dispatc hEventImpl (Component .java:2593 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:2497)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:339)
at java.awt.EventDispatchThre ad.pumpOne EventForHi erarchy(Ev entDispatc hTh
read.java:131)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hre
ad.java:98)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:9 3)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:85)
/***********************
//Write your array of Records:
MyRecordClass[] aRecs = new MyRecordClass[10];
for (int i = 0; i < 10; i++)
aRecs[i] = new MyRecordClass();
}
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:\\myfi
oos.writeObject(aRecs);
oos.close();
/***********************
But when I tried to do it I got the following error.
Exception occurred during event dispatching:
java.security.AccessContro
yfile.obj write)
at java.security.AccessContro
xt.java:272)
at java.security.AccessContro
399)
at java.lang.SecurityManager.
at java.lang.SecurityManager.
at java.io.FileOutputStream.<
at java.io.FileOutputStream.<
at Clientes.GrabaFile(Cliente
at Clientes.actionPerformed(C
at java.awt.Button.processAct
at java.awt.Button.processEve
at java.awt.Component.dispatc
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
read.java:131)
at java.awt.EventDispatchThre
ad.java:98)
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
For objects to be written using ObjectOutputStream then the class needs to implement the Serializable interface.
From the exception it appears the inner class Clientes$Usuario does not implement Serializable.
From the exception it appears the inner class Clientes$Usuario does not implement Serializable.
ASKER
Thanx it worked, but when I try to retrieve it using
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:\\myfil e2.obj"));
Clientes = (Clientes.Usuario[])ois.re adObject() ;
ois.close();
}
catch(IOException ioe){
System.out.println("Some kind of File Error happened!!!");
ioe.printStackTrace();
}
I'm getting this error when I compile it:
E:\Edatos\EDATOSPROY01\cli entes\Clie ntes.java: 314: unreported exception java.lang.ClassNotFoundExc eption; must be caught or declared to be thrown
Clientes = (Clientes.Usuario[])ois.re adObject() ;
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:\\myfil
Clientes = (Clientes.Usuario[])ois.re
ois.close();
}
catch(IOException ioe){
System.out.println("Some kind of File Error happened!!!");
ioe.printStackTrace();
}
I'm getting this error when I compile it:
E:\Edatos\EDATOSPROY01\cli
Clientes = (Clientes.Usuario[])ois.re
Are you trying to read the file written by the code above?
If so, then you should be doing:
MyRecordClass[] recs = (MyRecordClass[]) ois.readObject();
If so, then you should be doing:
MyRecordClass[] recs = (MyRecordClass[]) ois.readObject();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
grant {permission ava.io.FilePermission "e:\\*", "write";};
Now I'm getting this error:
java.io.NotSerializableExc
at java.io.ObjectOutputStream
at java.io.ObjectOutputStream
at java.io.ObjectOutputStream
at java.io.ObjectOutputStream
putStream.java:456)
at java.io.ObjectOutputStream
at Clientes.GrabaFile(Cliente
at Clientes.actionPerformed(C
at java.awt.Button.processAct
at java.awt.Button.processEve
at java.awt.Component.dispatc
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
read.java:131)
at java.awt.EventDispatchThre
ad.java:98)
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre