Java将输入附加到文件

Java将输入附加到文件,java,filewriter,Java,Filewriter,我的老师让我做这个练习来创建一个计算器,它可以将操作存储到文件中,并在以后读出。我有作家和计算器;但是编写器以某种方式覆盖了文件中的所有内容,而不是添加。我尝试了bufferedWrtier append,现在我正在尝试Printwriter 调试表明我的列表已正确填写;但结果是,保存的文件只包含最后一个输入编号和最后一个输入运算符 public class Rechnung { static Scanner scanner = new Scanner(System.in); static do

我的老师让我做这个练习来创建一个计算器,它可以将操作存储到文件中,并在以后读出。我有作家和计算器;但是编写器以某种方式覆盖了文件中的所有内容,而不是添加。我尝试了bufferedWrtier append,现在我正在尝试Printwriter

调试表明我的列表已正确填写;但结果是,保存的文件只包含最后一个输入编号和最后一个输入运算符

public class Rechnung {
static Scanner scanner = new Scanner(System.in);
static double zahl1, zahl2, ergebnis;
static String op;
static boolean abbruch = true;
static File fileName = new File("Therme.txt");
static String numbers1;
static String eingabe;

public static void eingabe() {

    while (abbruch) {
        System.out.println("Geben Sie eine Zahl ein: ");
        Listen.numbersDouble.add(scanner.nextDouble());
        System.out.println("Bitte entscheiden Sie sich für '+', '-', '/', '*', '=': ");
        op = scanner.next();
        Listen.operators.add(op);

        if (op.equals("=")) {
            for (int i = 0; i < Listen.operators.size(); i++)
            {
            Writer.write(String.valueOf(Listen.numbersDouble.get(i)), Listen.operators.get(i));
            }
            abbruch = false;

        }

        else {
        }

    }
    System.out.println(ausgabe());
}

public static double rechnen(String op, double zahl1, double zahl2)

{

    switch (op) {
    case "+":
        ergebnis = zahl1 + zahl2;
        return ergebnis;
    case "-":
        ergebnis = zahl1 - zahl2;
        return ergebnis;
    case "/":
        ergebnis = zahl1 / zahl2;
        return ergebnis;
    case "*":
        ergebnis = zahl1 * zahl2;
        return ergebnis;
    }
    return ergebnis;
}

public static double ausgabe() {

    zahl1 = Listen.numbersDouble.get(0);

    for (int i = 1; i <= Listen.numbersDouble.size(); i++)

    {
        op = Listen.operators.get(i - 1);

        if(op.equals("=")) 
                {
            return zahl1;
                }
        zahl2 = Listen.numbersDouble.get(i);
        zahl1 = Rechnung.rechnen(op, zahl1, zahl2);


    }
    return -80085;
}
}


请注意,我只有几周的编程经验,不知道任何编程惯例

若要将内容附加到现有文件,请在附加模式下打开file writer,方法是将第二个参数作为true传递

 fw = new FileWriter(FILENAME,true);
 bw = new BufferedWriter(fw);
 PrintWriter out = new PrintWriter(bw);
 out.println(String.valueOf(string));
 out.println(op);

您需要在Writer类中使用FileWriter的构造函数(File,boolean append)。

使用此构造函数,而不是用于创建
FileWriter
新FileWriter(FILENAME,true)的构造函数

这是这个构造函数的java文档,请阅读它的append参数

/**
 * Constructs a FileWriter object given a file name with a boolean
 * indicating whether or not to append the data written.
 *
 * @param fileName  String The system-dependent filename.
 * @param append    boolean if <code>true</code>, then data will be written
 *                  to the end of the file rather than the beginning.
 * @throws IOException  if the named file exists but is a directory rather
 *                  than a regular file, does not exist but cannot be
 *                  created, or cannot be opened for any other reason
 */
您需要在Filewriter构造函数中使用“append”参数,该参数的值必须为true

FileWriter fwr = new FileWriter(FILEN,true); 
可能重复的
FileWriter fwr = new FileWriter(FILEN,true);