Link to home
Start Free TrialLog in
Avatar of youngs101497
youngs101497

asked on

Write to a file?

I am just learning Java and I am having a problem with Writing to a file.  The class which does the write looks like the following:

//-
import java.io.*;

public class write_bytes {

    public static void main(/*String args[]*/)
    {
        int p_array[] = {1, 2, 3, 5, 7, 11, 13, 17};

        try {
            FileOutputStream os = new FileOutputStream("bytewrite.dat");

            for (int i = 0; i < p_array.length; i++)
              os.write(p_array[i]);
            os.close();
        }
        catch (IOException e)
        {
            System.out.println("File error: " + e);
        }
    }
}
//-

The code is actualy from a book.  It compiles fine but when I run it I get the following Error in the Console.

# Security Exception: checkwrite:bytewrite.dat
# Applet exception: security violation: security.checkwrite: bytewrite.dat
netscape.security.AppletSecurityException: security.checkwrite: bytewrite.dat
#  UniversalFileWrite privilege not enabled: Modifying files stored in your computer

What is causing this "security violation?"  How Do I write to a file using Java?

AJY
ASKER CERTIFIED SOLUTION
Avatar of jpk041897
jpk041897

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of youngs101497
youngs101497

ASKER

I find it hard to believe that you can't write to a file from an applet.  Is that what you were saying?  IF it is what is a "Sandbox," "Jar file" and a "class 2 signature?"
The general idea of an applet is that it will safeguard your computer against virus, trojans, etc. One of the ways it does that is by not allowing you to read or write files from the client machine. So yes, an applet cannot ordinarily write to a file.

The security Sandbox is a part of Java (run by the browser) that is responsible for making sure that your applet does not attempt to violate the applet security restrictions.

A JAR (Java ARchive) is a non compressed zip file that can: a) decrease load time by loading all class files in a single operation rather than as needed and b) Allow you to add a signature file (also known as certificate) so that a user may decide if he trusts you enough to allow your applet to bypass the sandbox.

A class 2 signed file is a Class 2 (I.e. Corporate level) security certificate that you may purchase from verisign or other vendors. Class 1 allows you to sign and encrypt e-mail, class 2 allows you  to sign applets.

You might want to look at:

http://www.mcp.com under the Personal Bookshelf Programming Bookshelf links. You will find several online books on Java at this address that will aid you in undestanding these (and other ) topics.