Java:如何在循环中不断更新文件

时间:2014-10-20 06:44:06

标签: java

我正在编写需要在循环中不断更新文本文件的代码:删除实际内容并编写另一个内容。问题是未正确完成更新。它使用以前版本的文件。

例如,在循环4中,被调用文件是在循环3中更新的文件。

我的问题是如何正确更新我的文件?

public static void main(String[] args) throws IOException {
for(int level:levels){
            RecipeTree.CloneTree(root,  partialroot);
            for(int i =0; i<100; i++){
                RecipeTree.removalcondition = RecipeTree.levelOfConditions(depth, length, recipe, level);
                System.out.println(removalcondition);
                RecipeTree.PartialTree(partialroot, RecipeTree.removalcondition);
                // Here the update of the file
                InitSTRIPSPlanner(partialroot);
                for(RecipeTree leaf: partialroot.getLeaves()){
                    ...........
                }    
            }   
        }
public static void InitSTRIPSPlanner(RecipeTree root) throws IOException{
        String adresseBut = System.getProperty("user.dir") + "/prolog/test-2p/Domain_knowledge.pl";
        String adresseSource = System.getProperty("user.dir") + "/prolog/test-2p/STRIPS_planner.pl";
        try {
            copyFileUsingStream(new File(adresseSource), new File(adresseBut));
        } catch (IOException e) {
            e.printStackTrace();
        }
        File fw = new File (adresseBut);
        OutputStream output = new FileOutputStream(fw,true);
        output.write("\n".getBytes());
        output.flush();
        FromTreeToProlog(root,output);
        output.close();
    }

    public static void FromTreeToProlog(RecipeTree root, OutputStream output) throws IOException{
        for(RecipeTree leaf: root.getLeaves()){
            if (leaf.getHead().getPostconditions() != null) { 
                output.write("\n".getBytes());
                output.flush();
                output.write("\n".getBytes());
                output.flush();
                if (leaf.getHead().getPreconditions() != null) {
                    output.write(("strips_preconditions("
                            + leaf.getHead().getName().toLowerCase() + ",["
                            + leaf.getHead().getPreconditions().toLowerCase()+ "]).").getBytes());
                    output.write("\n".getBytes());
                    output.flush();
                }
                else {
                    output.write(("strips_preconditions("
                            + leaf.getHead().getName().toLowerCase() + ",[_]).").getBytes());
                    output.write("\n".getBytes());
                    output.flush();
                }

                output.write(("strips_achieves("
                        + leaf.getHead().getName().toLowerCase() + ","
                        + leaf.getHead().getPostconditions().toLowerCase()
                        + ").").getBytes());
            }
        }
        for (String i : conditions) {
            output.write(("strips_primitive(" + i.toLowerCase() + ").").getBytes());
            output.write("\n".getBytes());
            output.flush();
        }
        for (String recipe : RecipeTree.RecipeCondition) {
            output.write(("strips_preconditions(" + recipe.toLowerCase() + ",[_]).").getBytes());
            output.write("\n".getBytes());
            output.flush();
            output.write(("strips_achieves(" + recipe.toLowerCase() + ",c"
                    + recipe.toLowerCase() + ").").getBytes());
            output.write("\n".getBytes());
            output.flush();
            output.write(("strips_primitive(c" + recipe.toLowerCase() + ").").getBytes());
            output.write("\n".getBytes());
            output.flush();
        }
    }

    private static void copyFileUsingStream(File source, File dest) throws IOException {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(source);
            os = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
        } finally {
            is.close();
            os.close();
        }
    }           

}

1 个答案:

答案 0 :(得分:0)

new FileOutputStream(fw, true);

追加,应该是

之一
new FileOutputStream(fw, false);
new FileOutputStream(fw);

如果我理解正确的话。

顺便说一句。

copyFileUsingStream(new File(adresseSource), new File(adresseBut));

可能是(自Java 7起):

Files.copy(Paths.get(adresseSource), Paths.get(adresseBut),
    StandardCopyOption.REPLACE_EXISTING);