Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to save a file with records.

Posted on 2002-07-06
5
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 900 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

688 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