/** * By default, this applet raises a security exception, unless * you configure your policy to allow applets from its location * to write to the file "writetest". */ import java.awt.*; import java.io.*; //import java.lang.*; import java.applet.*; import java.util.Date; import java.security.*; public class WriteFile extends Applet { String myFile = "D:\\Testing\\writetest.txt"; // The file to write File f = new File(myFile); DataOutputStream dos; String message = ""; public void init() { String osname = System.getProperty("os.name"); System.out.println("os.name is " + osname); System.out.println("Java version is " + System.getProperty("java.version")); // Following added for tests with IE7 - Still does not allow wildcards in .java.policy file /* grant codeBase "file:/D:/JavaDevelopment/Testing/WriteFile/-" { /* permission java.io.FilePermission "D:/Testing/writetest.txt", "read, write"; IE7 works*/ /* permission java.io.FilePermission "D:/Testing/*", "read, write"; // IE7 fails }; */ AccessController.doPrivileged(new PrivilegedAction() { public Object run() { System.out.println("Starting PrivilegedAction"); try { dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myFile),128)); dos.writeBytes("Cats can hypnotize you when you least expect it\n" + new Date() + "\n"); dos.flush(); dos.close(); message = "Successfully wrote to the file named " + myFile + " -- go take a look at it!"; } catch (SecurityException e) { message = "writeFile: ex: " + e; e.printStackTrace(); } catch (IOException ioe) { message = "writeFile: caught i/o exception " + ioe; ioe.printStackTrace(); } repaint(); // go show message return null; } }); } // end init() public void paint(Graphics g) { g.drawString(message, 10, 10); // show the message /* try { dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myFile),128)); dos.writeBytes("Cats can hypnotize you when you least expect it\n" + new Date() + "\n"); dos.flush(); g.drawString("Successfully wrote to the file named " + myFile + " -- go take a look at it!", 10, 10); } catch (SecurityException e) { g.drawString("writeFile: ex: " + e, 10, 10); } catch (IOException ioe) { g.drawString("writeFile: caught i/o exception", 10, 10); } */ } }