堆栈值未被读入文件

Stack values not being read into file

我目前正在研究以下方法:

public void addExpenditure(Stack<Double> costStack){

    try(BufferedWriter bw = new BufferedWriter(new FileWriter(f.getPath(), true))) {

        while (costStack.size() != 0)
            bw.write(costStack.pop().toString() + " ,");

    }catch(Exception e){
        e.printStackTrace();
    }

}

方法就是简单的把栈中存储的值costStack取出来写入文件。然而,由于未知原因,这并没有发生。

相反,每次都没有向文件写入任何内容。这很奇怪,因为 this 方法完全按照它应该做的,而且几乎相同:

public void addExpenditure(double amount){

    Double amount1 = amount;

    try(BufferedWriter bf = new BufferedWriter(new FileWriter(f.getPath(), true))){
        bf.write(amount1.toString() + " , ");
    }catch(IOException e){
        e.printStackTrace();
    }

}

也没有抛出异常。

这是我目前使用的所有方法"work":

import java.util.Scanner;
import java.util.Stack;

public class BudgetMain {

/*
Pre-Tax income per month: 75
After-Tax income per month: 12
Savings Goal per month (61% of After-Tax income): 08.62
Expendable income per month (After-Tax income - Savings Goal Per month): 04
 */

public static void main(String[] args){

    try {
        Budget b = new Budget();
        b.clearFile();
        System.out.println("Available funds left to spend this month: " + b.availableFundsLeft());
        Stack<Double> costStack = new Stack<>();
        costStack = takeValues(costStack);
        b.addExpenditure(costStack);
        System.out.println("Available funds left to spend this month: " + b.availableFundsLeft());
    }catch(Exception e){e.printStackTrace();}

    }


public static Stack<Double> takeValues(Stack<Double> costStack){

    System.out.println("Enter appropriate expenditures");
    Scanner stdin = new Scanner(System.in);

    while(true) {
        costStack.push(stdin.nextDouble());
        if (costStack.peek() == -1){
            costStack.pop();
            return costStack;
        }
    }


}

}

不同的 class 文件:

public void addExpenditure(Stack<Double> costStack){

    try(BufferedWriter bw = new BufferedWriter(new FileWriter(f.getPath(), true))) {

        while (costStack.size() != 0)
            bw.write(costStack.pop().toString() + " ,");

    }catch(Exception e){
        e.printStackTrace();
    }

}

public double availableFundsLeft(){
    try{return afterTaxIncome - savingsGoalPerMonth - 
getTotalExpenditureThisMonth();}
    catch(Exception e){e.printStackTrace();}
    return -1;
}

此代码似乎有效,它创建了一个内容为 2.0 ,1.0 ,

的 test.out 文件
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;

public class BudgetMain {


    public static void main(String[] args){
        Stack<Double> stack = new Stack<>();
        stack.push(1.0);
        stack.push(2.0);
        addExpenditure1(stack, new File("test.out"));
    }


    public static void addExpenditure1(Stack<Double> costStack, File f){

        try(BufferedWriter bw = new BufferedWriter(new FileWriter(f.getPath(), true))) {

            while (costStack.size() != 0)
                bw.write(costStack.pop().toString() + " ,");

        }catch(Exception e){
            e.printStackTrace();
        }

    }
}