Java - 将文本附加到文件末尾

时间:2014-02-20 01:58:07

标签: java file text printwriter bufferedwriter

我试图写入一个文本文件,该文件只包含我计划稍后处理的大块文本。我必须处理的各种变量组合以及全部输入都太繁琐了。我已经尝试过两次运行此代码,它所做的就是一遍又一遍地覆盖第一行,为我提供最后一组变量。这是代码。

import java.util.*;
import java.io.*;
public class FileWritingThing {
    public static void main(String[]args) throws IOException{
        String precision = null;
        String criteria = null;
        String specLevel = null;
        String precondLevel = null;
        PrintWriter writer = null;      

        for(int i = 0; i < 3; i++){
            if(i == 0){
                precision = "Precision = 4;";
            }
            if(i == 1){
                precision = "Precision = 8;";
            }
            if(i == 2){
                precision = "Precision = 12;";
            }
            for(int j = 0; j < 3; j++){
                if(j == 0){
                    criteria = "Objective Function;";
                }
                if(j == 1){
                    criteria = "Domain;";
                }
                if(j == 2){
                    criteria = "Objective Function + Domain;";
                }
                for(int k = 0; k < 3; k++){
                    if(k == 0){
                        specLevel = "Speculation Level = 10000;";
                    }
                    if(k == 1){
                        specLevel = "Speculation Level = 100000;";
                    }
                    if(k == 2){
                        specLevel = "Speculation Level = 1000000;";
                    }
                    for(int l = 0; l < 3; l++){
                        if(l == 0){
                            precondLevel = "Precondition Level = 0;";
                        }
                        if(l == 1){
                            precondLevel = "Precondition Level = 25;";
                        }
                        if(l == 2){
                            precondLevel = "Precondition Level = 100;";
                        }
                        writer.println(precision + "\n" + criteria + "\n" + specLevel + "\n" + precondLevel + "\n");
                    }
                }
            }
        }
    writer.close();
}

}

/*try {
    writer = new PrintWriter(new BufferedWriter(new FileWriter("permutations.txt", true)));
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
*/

我需要知道的是如何将每个块附加到文件行。另外,即使我已经包含&#34; \ n&#34;我也会遇到一些奇怪的问题。为了分离变量列表,无论如何都显示在一行上。底部的注释代码最初包含在&#34; writer.println()&#34;之前。线。

1 个答案:

答案 0 :(得分:1)

如果注释行位于writer.println()的上方,那么它可能会解释您遇到的奇怪行为。您应该在for循环外创建PrintWriter。像这样的东西

PrintWriter writer = null;
try {
   writer = new PrintWriter(new BufferedWriter(new FileWriter("permutations.txt", true)));
    // Most outer for loop goes here
    for(int i=0......) {
    }
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch(Exception e) { e.printStackTrace(); }
finally {
    if(writer!=null) {
        try {
            writer.flush();
            writer.close();
        } catch(Exception ignore) {}
    }
}