read and writing to files!!

Im trying to write to and read from a file using an object:

Write to a file:

try{

File outfile=new File("object.dat");

if(outfile.exists())

FileOutputStream ostream = new FileOutputStream(outfile);

ObjectOutputStream oostream = new ObjectOutputStream(ostream);

oostream.writeObject(aPerson);

ostream.close();

oostream.close();

}

catch(Exception e)

{

area1.setText("File Error");//displays message in text area on applet.

}

Read from File:

try{

File infile=new File("object.dat");

if(infile.exists())

FileInputStream istream = new FileInputStream(infile);

ObjectInputStream iistream = new ObjectInputStream(istream);

Person aPerson=(Person)iistream.readObject();

istream.close();

iistream.close();

}

catch(Exception e)

{

area1.setText("error");//displays message in textarea on applet.

}

The above code is incorrect:

1.What kid of file extension should i use with the file im writing to?(tried object.txt and object.dat)?

2.My applet has a save and load button with the above code used for each respectivly, the applet

will run but the buttons wont. i get this message in output window when i try to save to file:

java.security.AccessControlException: access denied (java.io.FilePermission object.dat read)

at java.security.AccessControlContext.checkPermission(AccessControlContext.java:270)

at java.security.AccessController.checkPermission(AccessController.java:401)

at java.lang.SecurityManager.checkPermission(SecurityManager.java:542)

at java.lang.SecurityManager.checkRead(SecurityManager.java:887)

at java.io.File.exists(File.java:677)

at MEMBERSHIP.PersonApplet.load1_ActionPerformed(PersonApplet.java:171)

at MEMBERSHIP.PersonApplet$ButtonListener.actionPerformed(PersonApplet.java:125)

at java.awt.Button.processActionEvent(Button.java:381)

at java.awt.Button.processEvent(Button.java:350)

at java.awt.Component.dispatchEventImpl(Component.java:3598)

at java.awt.Component.dispatchEvent(Component.java:3439)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)

3.Any help is creatly appreciated.

Thank you.

[2729 byte] By [billie493] at [2007-9-30 3:50:53]
# 1

The file extension of the file you're writing doesn't matter. You can call it whatever you like, as long as it doesn't violate the rules of your file system. For example, it's name can't be longer than the maximum length for your file system and the name can not include characters that are illegal in file names on your file system.

The exception you're getting doesn't appear to have anything to do with the file name. Try reading the 'Security in 1.2' chapter of the Java Tutorial. I think you need a policy file to give your applet the authority to read/write files. The Security chapter explains how to do that.

Rhino

rhino1c at 2007-6-29 17:09:11 > top of Java-index,Archived Forums,Java Programming...
# 2

> 1.What kid of file extension should i use with the file im writing to?(tried object.txt and object.dat)?

It's irrelevant; use whatever you like.

Since you can't open the serialized objects with a text editor, neither txt nor dat seem appropriate (though whether dat is a text data file or binary varies with application).

> 2.My applet has a save and load button with the above code used for each respectivly, the applet

> will run but the buttons wont. i get this message ge in output window when i try to save to file:

>

> java.security.AccessControlException: access denied (java.io.FilePermission object.dat read)

See the applet security faq,

http://java.sun.com/j2se/1.4.2/docs/guide/plugin/developer_guide/security.html

You cannot read or write files from an un-signed applet.

PeteKirkham at 2007-6-29 17:09:11 > top of Java-index,Archived Forums,Java Programming...