如何实例化所有方法都可以使用的 PrintStream,并将附加到同一个文件?

How do I instantiate a PrintStream that all methods can use, and will append to the same file?

我已经搜索了一段时间,但找不到我理解的方法。我得到的答案涉及几行代码和不同的打印机对象(FileOutputStream、FileWriter、bufferedwriter 等) PrintStream ps 在程序的其他两个方法中使用。当我在每个方法中实例化时它工作正常,但后来我无法附加到文件,它只是覆盖它。有没有一种简单的方法可以使用 class 变量来做到这一点?

import java.util.Scanner;
import java.io.PrintStream;

public class Main {

    public static void main(String[] args) throws Exception {

    Scanner sc = new Scanner(System.in);
    PrintStream ps = new PrintStream("brooklynCarpet.txt");


    double length, width, price, discount, area, carpetPrice, labor, 
    laborCost, installPrice, discountAmt, subTotal, tax, grandTotal;

            System.out.println("Length of room (feet)?");
            length = sc.nextDouble();

            System.out.println("Width of room (feet)?");
            width = sc.nextDouble();

            System.out.println("Customer discount (percent)?");
            discount = sc.nextDouble();

            System.out.println("Cost per square foot (xxx.xx)?");
            price = sc.nextDouble();

    area = length * width;
    carpetPrice = calcPrice(area, price);
    laborCost = calcLabor(area);
    installPrice = calcInstall(laborCost, carpetPrice);
    discountAmt = calcDisc(installPrice, discount);
    subTotal = calcSub(installPrice, discountAmt);
    tax = calcTax(subTotal);
    grandTotal = subTotal + tax;
    labor = 0.35;

    printHeader(length, width, area);

    printBill(price, carpetPrice, labor, laborCost, installPrice,
    discount, discountAmt, subTotal, tax, grandTotal);

}


// first function calculates carpet price.

public static double calcPrice(double area, double price) {

double carpetPrice;

carpetPrice = area * price;

return carpetPrice;

}

// second function calculates labor cost

public static double calcLabor(double area) {

    double labor = 0.35;

    double laborCost = area * labor;

    return laborCost;
}

// third function calculates installed price ie. carpet + labor

public static double calcInstall(double carpetPrice, double laborCost) {

    double installedPrice = carpetPrice + laborCost;

    return installedPrice;
}

// fourth function calculates discount ie % 0.01 * installed

public static double calcDisc(double installedPrice, double discount) {

    double discountAmt = installedPrice * (discount * 0.01);

    return discountAmt;
}

// fifth function calculates subtotal ie installed - discountamt

public static double calcSub(double installedPrice, double discountAmt) {

    double subTotal = installedPrice - discountAmt;

    return subTotal;
}

// sixth function calculates tax. subTotal * 0.085

public static double calcTax(double subTotal) {

    double taxRate = 0.085;

    double tax = subTotal * taxRate;

    return tax;
}

//想用下一个函数打印

public static void printHeader(double length, double width, double area) throws Exception{

    ps.printf("CARPET STORE\n");
    ps.printf("\n\n\t\t\tMEASUREMENT");
    ps.printf("\n\nLength\t\t\t\t %.2f feet", length);
    ps.printf("\n\nWidth\t\t\t\t %.2f feet", width);
    ps.printf("\n\nArea\t\t\t\t %.2f square feet", area);

}

// 还有这个

public static void printBill(double price, double carpetPrice, double labor
, double laborCost, double installPrice, double discount, 
double discountAmt, double subTotal, double tax, double grandTotal) throws Exception{

    ps.printf("\t\t\t\t\t  CHARGES\n\nDESCRIPTION\t\t\tCOST/SQ.FT.\t\t\tCHARGE/ROOM");
    ps.printf("\nCARPET\t\t\t\t\t" + price + "\t\t\t\t\t" + carpetPrice);
    ps.printf("\nLabor\t\t\t\t\t" + "%.2f" + "\t\t\t   $" + "%.2f", labor, laborCost);
    ps.printf("\n\t\t\t\t\t\t\t\t\t\t----------");
    ps.printf("\nINSTALLED PRICE\t\t   " + "$%.2f", installPrice);
    ps.printf("\nDiscount\t\t\t\t" + "%.2f" + " %%" + "\t\t\t\t%.2f" ,discount, discountAmt);
    ps.printf("\nSUBTOTAL\t\t\t\t\t\t\t\t\t" + "%.2f\n", subTotal);
    ps.printf("\nTax\t\t\t\t\t\t\t\t\t\t    " + "%.2f\n", tax);
    ps.printf("\nTotal\t\t\t\t\t\t\t\t\t   $" + "%.2f\n", grandTotal);

}

    }

我想你有三个选择,

选项 1:在追加模式中将 PrintStream 包裹在 FileOutputStream 周围。

选项 2:制作一个 PrintStream 全局变量。

选项 3:使用 System.setOut(PrintStream) - 然后您可以在任何地方调用 System.out.printf 来写入您的 PrintStream(老实说,这只是执行选项 2 的另一种方法)。

只需将 ps 声明为 Main class 的 static 成员,而不是 main 方法的局部变量:

public class Main {
    private static PrintStream ps;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        try { 
            // Don't declare `ps` here; just initialize it...
            ps = new PrintStream("brooklynCarpet.txt"); 
            // remaining logic of `main` below...
        } catch (FileNotFoundException fnf){
            System.out.println("Unexpected exception: " + fnf.toString());
        }
    }
    // ...
 }

现在 Main class 中的任何 方法都可以访问 'ps`。