Since Applets run in sandbox mode in browsers, I am using AccessController.doPrivileged to write to a file. It writes to the file when I run it in Eclipse, but doesn't write when I access the applet in browser.
What am I missing? Here is the code:
public class HelloWorld extends Applet {
public void paint(Graphics g) {
AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
public Boolean run() {
try {
System.out.println(System.getProperty("user.home"));
String userHome = System.getProperty("user.home");
FileWriter fw = new FileWriter(userHome + File.separator
+ "test" + File.separator + "area.txt");
fw.write("The area is 20m");
fw.flush();
fw.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
return Boolean.TRUE;
}
});
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…