Java 将输入附加到文件

Java Append input to File

我的老师让我做这个练习来创建一个计算器,它可以将操作存储到一个文件中并在以后读出它们。我得到了作家,我得到了计算器;但是作者以某种方式覆盖了文件中的所有内容而不是添加。我尝试了 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;
}

}

public class Writer {

public static void write(String string, String op) {
final String FILENAME = "C:\Users\nowackan\eclipse-workspace\Test.txt"; {


    BufferedWriter bw = null;
    FileWriter fw = null;

    try {

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


        System.out.println("Done");

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            if (bw != null)
                bw.close();

            if (fw != null)
                fw.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }

    }

}

}

请注意,我只有几周的编程经验,不知道任何编程约定。

要将内容追加到现有文件,请将第二个参数作为 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) 的构造函数 class。

使用此构造函数而不是您用来创建 FileWriternew FileWriter(FILENAME, true);

的构造函数

这是此构造函数的 java 文档,请阅读它的附加参数,

/**
 * 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" 参数,该参数的值必须为真。

FileWriter fwr = new FileWriter(FILEN,true);