Solved

How to save a file with records.

Posted on 2002-07-06
5
204 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse formatting 6 87
Java Server Faces parameter pass? 6 48
Custom Java Application connects to database: Which one of the 20? 4 27
Java string replace 11 43
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 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

10 Experts available now in Live!

Get 1:1 Help Now