-2
我正在編寫需要不斷更新循環中的文本文件的代碼:刪除實際內容並編寫另一個文件。問題是更新沒有正確完成。它使用該文件的以前版本。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();
}
}
}
我想你可以拿出一個小樣本,所以你可以先看看你正在做更新的過程中右和第二等可以幫助你更好。 – 2014-10-20 06:46:19
我無法在所有的序言樹邏輯中看到連續文件更新。 – Deltharis 2014-10-20 08:23:06