Solved

How to save a file with records.

Posted on 2002-07-06
5
200 Views
Last Modified: 2010-03-31
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)
0
Comment
Question by:rvaldivia
  • 2
  • 2
5 Comments
 

Author Comment

by:rvaldivia
ID: 7134328
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)

0
 
LVL 92

Expert Comment

by:objects
ID: 7134334
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.
0
 

Author Comment

by:rvaldivia
ID: 7134338
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();

0
 
LVL 92

Expert Comment

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

MyRecordClass[] recs = (MyRecordClass[]) ois.readObject();
0
 
LVL 35

Accepted Solution

by:
girionis earned 300 total points
ID: 7134856
>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();

  You should catch a ClassNotFoundException. Either throw it with your ethod declaration where the Clients = ... codes is or either use a try catch block like:

try
{
    Client = (Client.Usuario[]) ois.readObject();
}
catch (ClassNotFoundException cnfe)
{
}

  Hope it helps.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now