Java安全管理器(Security Manager)
阿新 • • 發佈:2019-01-02
(1)建立一個SecurityManager的子類;
(2) 覆蓋一些方法。
import java.io.*;
public class TestSecurity
{
public static void main(Stringargs[])
{
try {
System.setSecurityManager(newPasswordSecurityManager("123456"));
} catch (SecurityException se) {
System.out.println("SecurityManager already set!");
}
try {
//DataInputStream fis = new DataInputStream(newFileInputStream("input.txt"));
BufferedReader fis = new BufferedReader(newFileReader("input.txt"));
//DataOutputStream fos = new DataOutputStream( newFileOutputStream("output.txt"));
BufferedWriter fos = new BufferedWriter(newFileWriter("output.txt"));
String inputString;
while ((inputString = fis.readLine()) != null) {
//fos.writeBytes(inputString);
//fos.writeByte('\n');
fos.write(inputString);
fos.write('\n');
}
fis.close();
fos.close();
} catch (IOException ioe) {
System.out.println("I/O failed for SecurityManagerTest.");
}catch(Exception e)
{
System.out.println(e.toString());
}
(2) 覆蓋一些方法。
import java.io.*;
public class TestSecurity
{
public static void main(Stringargs[])
{
try {
System.setSecurityManager(newPasswordSecurityManager("123456"));
} catch (SecurityException se) {
System.out.println("SecurityManager already set!");
}
try {
//DataInputStream fis = new DataInputStream(newFileInputStream("input.txt"));
BufferedReader fis = new BufferedReader(newFileReader("input.txt"));
//DataOutputStream fos = new DataOutputStream( newFileOutputStream("output.txt"));
BufferedWriter fos = new BufferedWriter(newFileWriter("output.txt"));
String inputString;
while ((inputString = fis.readLine()) != null) {
//fos.writeBytes(inputString);
//fos.writeByte('\n');
fos.write(inputString);
fos.write('\n');
}
fis.close();
fos.close();
} catch (IOException ioe) {
System.out.println("I/O failed for SecurityManagerTest.");
}catch(Exception e)
{
System.out.println(e.toString());
}