aspose file tools*
The moose likes Applets and the fly likes Help creating a .txt file using an applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Help creating a .txt file using an applet" Watch "Help creating a .txt file using an applet" New topic
Author

Help creating a .txt file using an applet

David Paris
Greenhorn

Joined: Dec 09, 2005
Posts: 2
Hi,
I am a high school senior with a little over 1 year experience with Java. I am trying to create and write to a .txt file within an applet. However, when my program is run, I get "java.security.AccessControlException: access denied (java.io.FilePermission Quiz.txt write)". I am trying to write a program that creates a Quiz text file. I believe the issue is that the file is being created in the wrong place. I am creating the file by using myOutFile = new PrintWriter(new FileWriter("Quiz.txt", b)); b is of the boolean data type. After thoroughly going through the classes FileWriter and PrintWriter I am unsure as to where the file is being created. I expected the method getCodeBase(), however, nowhere in the code did I see the method used to create the file within the same directory.

writer=new EasyWriter("Quiz.txt");

public EasyWriter(String fileName)
{
this(fileName, null);
}

/**
* Constructor. Creates a new file. If the file exists
* can append to it.
* @param fileName the name of the file to be created
* @param mode if equals to "app" opens in append mode
*/
public EasyWriter(String fileName, String mode)
{
myFileName = fileName;
myErrorFlags = 0;

try
{
myOutFile = new PrintWriter(
new FileWriter(fileName, "app".equals(mode)));
}
catch (IOException e)
{
myErrorFlags |= OPENERROR;
myFileName = null;
}
}


Any help is appreciated.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
Welcome to JavaRanch!

Applets are not allowed to read or write file on the local hard disk for security reasons. To do that, an applet either needs to be digitally signed, or you need to alter the local security policy on your computer. Details can be found on this page in the FAQ section.

The getCodebase() method returns the directory where the applet lives [I]on the server[/], not on the client computer. You can't use that easily to write files to. From your description it's not quite clear whether you are running this applet locally, or if there is a web server involved. If you provide some more details on why this needs to be an applet (as opposed to an application), we may be able to give some more hints.


Ping & DNS - my free Android networking tools app
David Paris
Greenhorn

Joined: Dec 09, 2005
Posts: 2
Ulf,
Thank you very much for you quick response to my problem.

I am running the applet locally right now for debugging purposes. However, the farthest I will be running the applet is on a local network.

The link you gave me showed me how to rewrite the policies for the applet, which I hope will solve my problem. Also, I was hoping to find out where the file is being created, especially when it is running on the local network, and how I can edit where the file is being created. In previous languages I've worked with I was able to specify where I was creating a file by using the file name's directory. However, in java the '\' is used for escape characters. While looking through the API, I have found some instances where "/*" and "/-" were used to specify a filename directory. Would this help me?

Once again, thank you very much for your input.
David
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
You can use backslashes in filenames by escaping them, e.g. a filename might be "C:\\foo\\bar\\baz.txt". That way you can control precisely where the file is created.
 
 
subject: Help creating a .txt file using an applet