Solved

Write to a file?

Posted on 1998-05-07
3
161 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:youngs101497
[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
3 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 20 total points
ID: 1220747
I assume, based on the error, that you are attempting to run this code from an Apllet. If so, you are actualy running into 2 problems.

1.- The code is not intended to be run from an applet bus as a standalone application. You must run it from the command line with:

Javac write_bytes.java

to compile the code and

Java write_bytes to run it.


The second problem is that even if the code had been intended for an Applet, the browsers security Sandbox would have stopped you. You would have to place the class file in a signed JAR file (signed with a class 2 signature at least) to bypass the security restriction.
0
 

Author Comment

by:youngs101497
ID: 1220748
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?"
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220749
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to install java on RHEL image on EC2 4 53
hibernate jars 4 57
ejb message driven bean mdb creation steps 2 34
Java class and jar 3 41
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

740 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