java.io.FileNotFoundException(打开的文件太多)FileReader与BufferedReader

时间:2019-03-09 19:46:00

标签: java file

Java代码:

for (int i=0; i<13000; i++){
    new FileReader("helloWorld.txt");
}

给予:

> Exception in thread "main" java.io.FileNotFoundException: helloWorld.txt (Too many open files)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
at java.base/java.io.FileReader.<init>(FileReader.java:60)
at Main.main(Main.java:14)

这还可以,因为没有足够的系统资源。但是我不知道为什么这样:

for (int i=0; i<13000; i++){
    new BufferedReader(new FileReader("helloWorld.txt"));
}

即使FileReader和BufferedReader之间存在许多差异,仍然存在:new FileReader(“ helloWorld.txt”)-那么它不应该像第一种情况那样会耗尽资源吗?

0 个答案:

没有答案