0

I am creating a applet which is implementing file writing operations in java, but it is getting some error like access denied in writing. The reading operation is not having any error. I have checked Changing java.policy file for applet which implements adding permissions to java.policy file but sadly it doesnt worked.

code:

    /* Java Program to Perform Read and Write Operations */

    import java.applet.*;

    import java.awt.*;

    import java.awt.event.*;

    import java.io.*;

    public class File extends Applet implements ActionListener

    {

        TextField file;

        TextArea text;

        //Initialize the applet

        public void init()

        {

      setBackground(Color.white);

      setLayout(null);

     

      Label label = new Label("File Name :");

      label.setBounds(100,0,100,40);

      this.add(label);

     

      file = new TextField();

      file.setBounds(200,0,200,40);

      this.add(file);

     

      text = new TextArea("",0,0,TextArea.SCROLLBARS_NONE);

      text.setBounds(50,75,400,300);

      this.add(text);

     

      Button read = new Button("Read From File");

      read.setBounds(75,400,150,25);

      this.add(read);

      read.addActionListener(this);

     

      Button write = new Button("Write To File");

      write.setBounds(275,400,150,25);

      this.add(write);

      write.addActionListener(this);

        }

        //Function to call functions for operations selected

        public void actionPerformed(ActionEvent e)

        {

      String button = e.getActionCommand();

      if(button.equals("Read From File"))

      {

          read();

      }

      else

      {

          write();

      }

        }

        //Function to Read the File

        public void read()

        {

      try

      {

          FileReader obj = new FileReader(file.getText());

          int i;

          text.setText("");

          while((i=obj.read())!= -1)

          {

         text.setText(text.getText()+(char)i);

          }

          obj.close();

      }

      catch(Exception E)

      {

          text.setText(E.getMessage());      

      }

        }

        //Function to Write to the File

        public void write()

        {

      try

      {
        System.out.println(System.getProperty("user.home"));

          FileWriter obj = new FileWriter(file.getText());

          obj.write(text.getText());

          obj.close();

          text.setText("File Written Successfully");

      }

      catch(Exception E)

      {

          text.setText(E.getMessage());

      }

        }

    }

    /*

    <applet code = File.class width=500 height=500>

    </applet>

    */

error:

access denied ("java.util.PropertyPermission" "user.home" "read")
Peter Alsen
  • 320
  • 2
  • 5
  • 15
  • 4
    The first most important question: why do you want to learn about applets? This is dead technology. So unless you engage in computer science history, you really shouldnt spend any time on applets. Seriously, waste of time. What is the goal you want to achieve using applets? – GhostCat Dec 04 '20 at 13:32
  • What is the goal you want to achieve using applets?- i want to give my java program some graphical interface for college projects. – Mayank Mohak Dec 04 '20 at 13:53
  • Yes, that is my question to you. What do you want to use your applet for? – GhostCat Dec 04 '20 at 13:55
  • 1
    [Use Swing instead](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Leave the Applet alone. I'm surprised schools are still teaching this stuff. They need to upgrade their textbooks. All newer textbooks 2015 and newer are already teaching JavaFX over Swing. – Paul Samsotha Dec 04 '20 at 14:01
  • Thank you for suggestion but can this error be solved for now. Or it ends with ending technology. – Mayank Mohak Dec 04 '20 at 14:05
  • 3
    https://stackoverflow.com/q/7406871/2587435. Not worth the time. You could probably learn how to build the same in Swing before you figure out how to do accomplish the same with an Applet. Working with Swing is pretty similar to working with AWT. The Swing documentation is actually pretty good with alot of examples. – Paul Samsotha Dec 04 '20 at 14:21

0 Answers0