package com.leasehouse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.AccessControlException;
public class TestForkJoin {
public static void main(String[] args) {
System.setSecurityManager(new MySecurityManager());
File file = new File("/Users/yulifan/Desktop/ff.exe");
try {
FileInputStream fileInputStream = new FileInputStream(file);
byte[] cc = new byte[1024];
//fileInputStream.read(cc);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MySecurityManager extends SecurityManager {
@Override
public void checkRead(String file){
if(file.endsWith("exe")) {
throw new AccessControlException("cannot read file:" + file);
}
super.checkRead(file);
}
}
上面的代码将会报错(并且先于判断文件是否存在之前执行):
java.security.AccessControlException: cannot read file:/Users/yulifan/Desktop/ff.exe
at com.leasehouse.MySecurityManager.checkRead(TestForkJoin.java:30)
at java.io.FileInputStream.<init>(FileInputStream.java:135)
at com.leasehouse.TestForkJoin.main(TestForkJoin.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
package com.leasehouse;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.security.AccessControlException;/** * Created by yulifan on 2017/7/26