Solved

How to save a file with records.

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 32
hibernate example issues from command prompt 10 50
null output 3 35
eclipse console opening separately 2 29
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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.

861 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