文件写入操作在applet中的java中出现错误

ljsrvy3e  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(258)

我正在创建一个applet,它正在java中实现文件写入操作,但是它遇到了一些错误,比如写入时拒绝访问。读取操作没有任何错误。我已经检查了为applet更改java.policy文件,applet实现了向java.policy文件添加权限,但遗憾的是它没有工作。
代码:

/* 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>

    */

错误:

access denied ("java.util.PropertyPermission" "user.home" "read")

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题