• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help creating a .txt file using an applet

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
David Paris
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic