Results 1 to 3 of 3

Thread: Applet

  1. #1
    sparlay_pk is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default Applet

    I am trying to read from one file and write to another, but the problem that i face is.

    java.security.AccessControlException: access denied (java.io.FilePermission FileDestination.txt write)


    My Code is this..


    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    import javax.swing.border.*;

    public class AppletClass extends JApplet implements ActionListener
    {
    private JPanel pane = null;
    private JScrollPane scrolling = null;
    private JTextPane fileBox = null;
    private JTextField tfFilename = null;
    private JButton butLoad = null;
    private final String LOAD = "Browse";

    public void init()
    {
    try
    {
    jbInit();
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    // method which will read data from file, and return it in
    // String
    public String readFile(String fn)
    {
    String thisLine, ret = "";
    try
    {
    FileInputStream fIn = new FileInputStream(fn);
    BufferedReader myInput = new BufferedReader(new InputStreamReader(fIn));

    //String desFile = JOptionPane.showInputDialog("Enter destination file", null);
    FileOutputStream fOut = new FileOutputStream("FileDestination.txt");
    PrintWriter myOutPut = new PrintWriter(new OutputStreamWriter(fOut));

    while ((thisLine = myInput.readLine()) != null)
    {
    ret += thisLine + "\n";
    myOutPut.println(thisLine);
    }
    }
    catch (Exception e)
    {
    ret = e.toString();
    }
    return ret;
    }

    private void jbInit() throws Exception
    {
    pane = new JPanel();
    pane.setBounds(new Rectangle(0, 0, 500, 325));
    pane.setLayout(null);

    fileBox = new JTextPane();
    fileBox.setText("");
    fileBox.setEditable(false);
    scrolling = new JScrollPane(fileBox);
    scrolling.setBounds(new Rectangle(16, 65, 295, 225));

    tfFilename = new JTextField();
    tfFilename.setText("");
    tfFilename.setBounds(new Rectangle(16, 23, 206, 29));

    butLoad = new JButton();
    butLoad.setBounds(new Rectangle(231, 23, 80, 30));
    butLoad.setText(LOAD);
    butLoad.setActionCommand(LOAD);
    butLoad.addActionListener(this);

    pane.add(scrolling);
    pane.add(tfFilename);
    pane.add(butLoad);

    setContentPane(pane);
    }

    public void actionPerformed(ActionEvent e)
    {
    if (e.getActionCommand().equals(LOAD))
    {
    Frame parent = new Frame();
    FileDialog fd = new FileDialog(parent, "Please choose a file:", FileDialog.LOAD);
    fd.show();
    String selectedItem = fd.getDirectory() + fd.getFile();
    tfFilename.setText(selectedItem);
    fileBox.setText(readFile(tfFilename.getText()));
    }
    }
    }




    My Applet.html file is

    <html>
    <applet code="AppletClass.class"width=325 height=325>
    </applet>
    </html>


    and my batch file is run.bat

    set path=C:\Program Files (x86)\Java\jdk1.6.0_18\bin
    javac AppletClass.java
    appletviewer MyApplet.html

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    Your applet needs permission to read/write files.
    Why do you want to do this in an applet vs an application?

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Emm...

    to use java.io.File or something like that with applet you need to sign the applet.
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

Similar Threads

  1. Replies: 2
    Last Post: 09-29-2009, 03:33 PM
  2. 3D Applet
    By DzzD in forum Java Applets
    Replies: 1
    Last Post: 03-30-2009, 11:20 PM
  3. Calling another applet on click of button in one applet
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 1
    Last Post: 02-19-2009, 12:54 PM
  4. need applet help !
    By fadiRU in forum Java Applets
    Replies: 2
    Last Post: 02-08-2009, 02:29 AM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •