由于小程序在浏览器中以沙盒模式运行,因此我使用 AccessController.doPrivileged 写入文件。当我在 Eclipse 中运行它时它会写入文件,但当我在浏览器中访问小程序时它不会写入。我错过了什么?这是代码:
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;
}
});
}
}