Results 1 to 2 of 2
  1. #1

    writing to and reading from 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.
    4. I also have and exit button which when pressed should close the applet:
    void exit1_ActionPerformed(ActionEvent event)
    {
    System.exit(0);
    }
    This is how i taught it should look but it doesnt work! any suggestions?

    Thank you.

  2. #2
    Join Date
    Dec 2002
    Location
    NY, NY
    Posts
    3,975
    So you are trying to read and then write to a file called "object.dat" right?


    Also you should use scopes on your ifs..
    for example..
    if(a == b)
    z();
    x();

    are you aware that if a is not equal to b, then x(); still gets run...
    when you dont include scopes.. the JVM does this:
    if(a == b){
    z();
    }
    x();

    So for your code...
    Code:
    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();
    }
    //If the file does not exist, there is no point of creating any outputstreams to it ... right?? lol
    
    }
    catch(Exception e)
    {
    area1.setText("File Error");//displays message in text area on applet.
    }
    Read from File:
    
    try{
    //same here....
    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();
    
    }
    Cant read from a file that doesnt exist.. can ya?
    }
    However i do not think that is your problem.
    I beleive you do not have write access to the file.
    Are you trying to have an applet run on the web and write to a users computer?
    This is not allowed by the JVM, as this would be a huge security issue.
    Imagine.. you could re-write their win.ini and other important system files.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •