Solved

Write to a file?

Posted on 1998-05-07
3
158 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
  • 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now