Authorized File Writer : Policy « Security « Java






Authorized File Writer

 
import java.io.FileWriter;
import java.io.IOException;

public class AuthorizedFileWriter {
  public static void main(String[] args) {
    System.setSecurityManager(new SecurityManager());
    String file = "authorized.txt";
    String fileBody = "test";
    try {
      FileWriter fileWriter = new FileWriter(file);
      fileWriter.write(fileBody);
      fileWriter.close();
      System.exit(0);
    } catch (IOException ioException) {
      ioException.printStackTrace();
      System.exit(1);
    }
  }

}

/*
// authorized.policy
// Policy file that grants file write permission 
// only to file "authorized.txt"

grant {
   permission java.io.FilePermission
     "authorized.txt", "write";
};

*/

/*

// codebase_authorized.policy
// Policy file that grants write permission to 
// file "codebase_authorized.txt" for codebase "C:/myclasses"

grant codebase "file:/C:/myclasses" {
   permission java.io.FilePermission
     "codebase_authorized.txt", "write";
};

*/

           
         
  








Related examples in the same category

1.System properties and security policy System properties and security policy
2.Managing Policy Files: By default, the JDK uses the policy files located in
3.Policy Demo
4.To specify an additional policy file, set the java.security.policy system property at the command line:
5.To ignore the policies in the java.security file, and use the specified policy, use '==' instead of '='