code to read local notepad.txt from applet

Hi.

Further to my (unanswered) queries about reading a notepad.txt file from the local directory i.e. the same directory from which the html and applet were launched, the following code is supposed to be able to read the local file. It failed to do so both in Netscape 7.1 and in IE5

I've also received conflicting advices : some says we can read local files without using a signed applet or chaning the ACL, whereas others I'll need to use signed applets and / or acl to allow the applet to read a local file.

My research has shown that java applets are supposed to be able to read local files. Has something changed:

publicclass FileAccessextends Applet

implements ActionListener{

Button loadButton, saveButton;

TextField filename;

TextArea content;

String browserName;

boolean securitySupported =false;

publicvoid init(){

setLayout(new FlowLayout());

Label label =

new Label("Simple file editor");

add(label);

loadButton =new Button("Load");

saveButton =new Button("Save");

add(loadButton);

add(saveButton);

loadButton.addActionListener(this);

saveButton.addActionListener(this);

filename =new TextField(20);

add(filename);

content =new TextArea(5,20);

add(content);

browserName = System.getProperty("java.vendor");

if (browserName.indexOf("Netscape") > -1)

securitySupported =true;

setSize(200, 200);

}

publicvoid actionPerformed(ActionEvent evt){

if (securitySupported){

if (evt.getSource() == saveButton){

PrivilegeManager.enablePrivilege("UniversalFileAccess");

try{

FileWriter aWriter =new FileWriter(filename.getText(),false);

aWriter.write(content.getText());

aWriter.flush();

aWriter.close();

}

catch(Exception e){

e.printStackTrace();

}

}

elseif (evt.getSource() == loadButton){

PrivilegeManager.enablePrivilege("UniversalFileAccess");

try{

BufferedReader aReader =

new BufferedReader

(new FileReader(filename.getText()));

content.setText(aReader.readLine());

aReader.close();

}

catch(Exception e){

e.printStackTrace();

}

}

else{

getAppletContext().showStatus("security not installed");

}

}

}

}

[4300 byte] By [acinhka] at [2007-9-29 20:13:43]
# 1

Hi acinhk,

I read your note with great interest. I am trying to solve almost exactly the same problem as you are -

Reading a .txt file in IE5 from the same directory where the html and the applet were lauched.

Your code looks fine to me.

My code is very similar, and again generates a security exception - as I am having the same problem you had getting around Java security.

If you have been able to solve the problem, can you please let me know what you did.

Any modifications to your sample code that actually made it work would be greatly appreciated.

Also, is signing the applet to read a local file under IE5 really necessary ?

Can the file be read from the local IE file system without signing the applet ?

Please let me know your thoughts.

Thanks !

drbob

Drbob2ua at 2007-7-16 0:26:35 > top of Java-index,Security,Signed Applets...