AuthorizedFileWriter.java Source code

Java tutorial

Introduction

Here is the source code for AuthorizedFileWriter.java

Source

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";
};
    
*/