都说文件操作,最后必须关闭文件流,但是我刚做了个实验,代码如下:
[code="java"]public static void readWithoutColse(int i,String fileName) throws Exception{
List fi=new ArrayList();
for(int k=0;k<i;k++){
FileInputStream fItem=new FileInputStream(new File(fileName));
System.out.println(fItem.read());
fi.add(fItem);
}
}
public static void main(String[] s) throws FileNotFoundException, IOException{
try {
//freshObj() ;
// testReserilizeObj();
readWithoutColse(10000000, "out.o");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} [/code]
结果这样都没有报出与文件打开过多的异常。
你重复执行10000000,都只是打开了同一个文件而已,你要是打开不同的文件,肯定会报错,不允许你同时持有这么多的文件句柄
另外,你打开文件用的是操作系统的读锁,你要是同时尝试写入,就会失败,因为写锁和读锁互斥