Link to home
Start Free TrialLog in
Avatar of rvaldivia
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:\\myfile.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.AccessControlException: access denied (java.io.FilePermission e:\m
yfile.obj write)
        at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:272)
        at java.security.AccessController.checkPermission(AccessController.java:
399)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:545)
        at java.lang.SecurityManager.checkWrite(SecurityManager.java:978)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:96)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:62)
        at Clientes.GrabaFile(Clientes.java:296)
        at Clientes.actionPerformed(Clientes.java:334)
        at java.awt.Button.processActionEvent(Button.java:329)
        at java.awt.Button.processEvent(Button.java:302)
        at java.awt.Component.dispatchEventImpl(Component.java:2593)
        at java.awt.Component.dispatchEvent(Component.java:2497)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:131)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:98)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)
Avatar of rvaldivia
rvaldivia

ASKER

I opened the java.policy file and added this line:
grant {permission ava.io.FilePermission "e:\\*", "write";};

Now I'm getting this error:

java.io.NotSerializableException: Clientes$Usuario
        at java.io.ObjectOutputStream.outputObject(ObjectOutputStream.java:1148)

        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:366)
        at java.io.ObjectOutputStream.outputArray(ObjectOutputStream.java:1098)
        at java.io.ObjectOutputStream.checkSubstitutableSpecialClasses(ObjectOut
putStream.java:456)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:361)
        at Clientes.GrabaFile(Clientes.java:299)
        at Clientes.actionPerformed(Clientes.java:336)
        at java.awt.Button.processActionEvent(Button.java:329)
        at java.awt.Button.processEvent(Button.java:302)
        at java.awt.Component.dispatchEventImpl(Component.java:2593)
        at java.awt.Component.dispatchEvent(Component.java:2497)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:131)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:98)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)

Avatar of Mick Barry
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.
Thanx it worked, but when I try to retrieve it using

try{
  ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:\\myfile2.obj"));
  Clientes = (Clientes.Usuario[])ois.readObject();
  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\clientes\Clientes.java:314: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
               Clientes = (Clientes.Usuario[])ois.readObject();

Are you trying to read the file written by the code above?
If so, then you should be doing:

MyRecordClass[] recs = (MyRecordClass[]) ois.readObject();
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial