Solved

Write to a file?

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

738 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