java.io.FileNotFoundException :(打开的文件太多)

时间:2013-11-12 17:51:45

标签: java fileoutputstream

一切!我正在编写一个使用Graphs运行的java程序。代码需要用几个图表做一些事情。它工作正常,但是现在,每次启动程序时,它都会在完全执行15个方法之后停止,该方法逐个处理图形。之后,我在'CreateAllVertexCritical'方法的以下行中收到此错误:

    FileWriter fr=new FileWriter(filename, true);

我找到的最有用的链接之一是这个(几乎相同的问题): https://forums.oracle.com/message/8642300 但是,我无法解决这个问题。我关闭了我创建的所有Streams [请,如果我不对,请纠正我]。我还尝试增加打开文件数量的UBUNTU限制,但它没有帮助。 以下是我在main方法中调用的两种方法:

 public synchronized static void CreateAllVertexCritical(File f, int nVertices)      throws     IOException {
    isReady=false;
    String s = f.getParent();
    String name = f.getName();
    File newDir = new File(s + "/Reduce" + name);
    newDir.mkdir();

    Path path = Paths.get(f.getAbsolutePath());
    InputStream in = Files.newInputStream(path);
    BufferedReader reader
            = new BufferedReader(new InputStreamReader(in));
    String line;
    line = reader.readLine();
    Matcher m = pat.matcher(line);
    m.find();
    m.find();
    String s1 = m.group(2);
    int edgeBr = Integer.parseInt(s1);
    //if (edgeBr<54) return;
    int newEdgeBr = edgeBr - 1;
    //System.out.println(newEdgeBr);
    String[] lastEdges = new String[edgeBr];

    String filename = newDir.getPath() + "/1";
    //File temp = new File(filename);
    // System.out.println(temp.exists());
    //System.out.println(filename);
    FileWriter fr1=new FileWriter(filename, true);
    pw = new PrintWriter(new BufferedWriter(fr1));
    //System.out.println("p edge " + nVertices + " " + newEdgeBr + "\n");
    pw.print(new String("p edge " + nVertices + " " + newEdgeBr + "\n"));

    line = reader.readLine();
    lastEdges[0] = line;
    for (int l = 2; l <= edgeBr; l++) {
        line = reader.readLine();
        pw.print(line + "\n");
    }
    pw.flush();
    fr1.close();
    for (int j = 2; j <= edgeBr; j++) {
        reader
                = new BufferedReader(new InputStreamReader(Files.newInputStream(path)));
        line = reader.readLine();
        filename = s + "/Reduce" + name + "/" + j;
        FileWriter fr=new FileWriter(filename, true);
        pw = new PrintWriter(new BufferedWriter(fr));
        pw.print(new String("p edge " + nVertices + " " + newEdgeBr + "\n"));
        for (int l = 1; l < j; l++) {
            line = reader.readLine();
            pw.print(line + "\n");
        }
        line = reader.readLine();
        lastEdges[j - 1] = line;
        for (int l = j + 1; l <= edgeBr; l++) {
            line = reader.readLine();
            pw.print(line + "\n");
        }
        pw.flush();
        fr.close();
    }

    for (int j = 1; j <= edgeBr; j++) {
        try {
            Process psmallk = Runtime.getRuntime().exec("/home/user/Desktop/SmallkSrc/smallk " + s + "/Reduce" + name + "/" + j + " 3 6");
            psmallk.waitFor();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        File normalfile = new File(s + "/Reduce" + name + "/" + j);
        File resfile = new File(s + "/Reduce" + name + "/" + j + ".res");
        if (normalfile.exists() && (resfile.exists())) {
            normalfile.delete();
            resfile.delete();

        } else {
            Path dirPath = Paths.get("/media/user/1898016F98014CA6/F725/Reduce/ForTest14");
            File dirFile = dirPath.toFile();
            int count = dirFile.listFiles(filter).length;
            count++;
            Path tempPath = Paths.get("/media/user/1898016F98014CA6/F725/Reduce/ForTest14/" + count);
            Files.copy(normalfile.toPath(), tempPath);
            //sSystem.out.println(lastEdges[j-1]);
            PrintWriter pwinfo = new PrintWriter(new BufferedWriter(new FileWriter(pinfo, true)));
            pwinfo.print(count + " :" + lastEdges[j - 1] + "\n");
            pwinfo.flush();
            //CreateAllVertexCritical(normalfile, nVertices);

        }



    }
    isReady=true;

}

public static void CreateAllVertexCriticalRecursive(File f, int n) throws IOException, InterruptedException {
    //if(f.isDirectory()==true) return;
    Path path = Paths.get(f.getAbsolutePath());
    InputStream in = Files.newInputStream(path);
    BufferedReader reader
            = new BufferedReader(new InputStreamReader(in));
    String line;
    line = reader.readLine();
    Matcher m = pat.matcher(line);
    m.find();
    m.find();
    String s1 = m.group(2);
    int edgeBr = Integer.parseInt(s1);

   CreateAllVertexCritical(f, n);
    while(isReady==false){TimeUnit.NANOSECONDS.sleep(100);}
    String s = f.getParent();
    String name = f.getName();
    File newDir = new File(s + "/Reduce" + name);

    File[] fileArr = newDir.listFiles(filter);
    int brFiles = fileArr.length;
    System.out.println(brFiles);
    if (brFiles>4) System.out.println(newDir.getAbsolutePath() +": "+ brFiles);

    if (brFiles > 0) {

        for (int i = 0; i < brFiles; i++) {
            //System.out.println("Fail "+ i+" "+ fileArr[i].getName());
            CreateAllVertexCriticalRecursive(fileArr[i], n);
        }
    }

}
 public static void main(String[] args) throws IOException, InterruptedException {

    for(int u=120;u<9241;u++){ //1936 do 2000 missed
        //if (u%3==0) System.gc(); 
        System.out.println(u);
        CreateAllVertexCriticalRecursive(new File("/media/user/1898016F98014CA6/F725/Temp/"+u), 14);
    }

1 个答案:

答案 0 :(得分:0)

重构所有此代码,以使用try-with-resources块管理所有Stream / Reader / Writer对象。