0

全部!我正在编写一个使用 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);
    }
4

1 回答 1

0

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

于 2015-07-15T13:29:17.043 回答