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");
}
}
}
}

