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
Solved

Write to a file?

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
arguments to jar 5 33
Bot application - advice 3 37
Need help aligning JRadioButton that put in a box using GridBagLayout 2 16
Java class and jar 3 20
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

789 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