写入文本文件然后将其读回文本区域 javafx

Writing into a text file then reading it back into a textarea javafx

我制作了一个将订单写入 txt 文件的 javafx 应用程序,然后我将 txt 文件读入文本区域。 我的代码正在运行并且正在打印文件,但我不知道如何正确地格式化它。我是 javafx 的菜鸟,我写错了吗?任何帮助表示赞赏

这是写入文件的代码的主要部分。

Date date = new Date( );
      SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz"); 

  BufferedWriter bf = new BufferedWriter(new FileWriter("receipt.txt")); 
  bf.write("*************SHERIDAN BAGEL SHOP*************,");
  bf.newLine();
  bf.write(ft.format(date));
  bf.newLine();
  bf.write("Item:\t\t\tQty\tAmount,");
  bf.newLine();
 bf.write("\t\t\t\t-----------");
        bf.newLine();
        bf.write("Pretax Total\t\t\t$"+df.format(cost)+",");
        bf.newLine();
        bf.write("Sales Tax 13%\t\t\t$"+df.format(calctax)+",");
        bf.newLine();
        bf.write("Total Sale\t\t\t$"+df.format(calctotal)+",");
        bf.newLine();
        bf.write("*********THANK YOU FOR YOUR ORDER*********,");

      bf.close();

这是文本文件中的输出

*************SHERIDAN BAGEL SHOP*************,
Sat 2017.04.01 at 01:06:57 PM EDT
Item:           Qty     Amount,
                      -----------
Pretax Total            [=11=].00,
Sales Tax 13%           [=11=].00,
Total Sale              [=11=].00,
*********THANK YOU FOR YOUR ORDER*********,

这是读取文件的代码

@FXML
    private TextArea receipt;

 public void ViewReceipt() {
     try {
        Scanner s = new Scanner(new File("receipt.txt"));
        while (s.hasNext()) {
          receipt.appendText(s.nextLine()+"\n");
        }
    } catch (FileNotFoundException ex) {
        System.err.println(ex);
    }


}

文本区域是这样的

*************SHERIDAN BAGEL SHOP*************,
Sat 2017.04.01 at 01:06:57 PM EDT
Item:           Qty Amount,
                -----------
Pretax Total            [=13=].00,
Sales Tax 13%           [=13=].00,
Total Sale          [=13=].00,
*********THANK YOU FOR YOUR ORDER*********,

你可以玩这个。如果所有的收据看起来都非常相似。

import java.io.*;
import java.text.*;
import java.util.*;
import java.util.logging.*;
import javafx.application.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

/**
 *
 * @author Sedrick
 */
public class JavaFXApplication22 extends Application {

    TextArea textArea;

    @Override
    public void start(Stage primaryStage)
    {
        textArea = new TextArea();

        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event)
            {
                Date date = new Date();
                SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
                String pattern = "$###,###.###";
                DecimalFormat df = new DecimalFormat(pattern);

                try (BufferedWriter bf = new BufferedWriter(new FileWriter("receipt.txt")))
                {
                    bf.write("*************SHERIDAN BAGEL SHOP*************,");
                    bf.newLine();
                    bf.write(ft.format(date));
                    bf.newLine();
                    bf.write(writeSpace(25, "Item:") + writeSpace(12, "Qty") + "Amount,");
                    bf.newLine();
                    bf.write(writeSpace(35, "") + "-----------");
                    bf.newLine();
                    bf.write(writeSpace(35, "Pretax Total") + df.format(100.0) + ",");
                    bf.newLine();
                    bf.write(writeSpace(35, "Sales Tax 13%") + df.format(10000.0) + ",");
                    bf.newLine();
                    bf.write(writeSpace(35, "Total Sale") + df.format(10.0) + ",");
                    bf.newLine();
                    bf.write("*********THANK YOU FOR YOUR ORDER*********,");
                }
                catch (IOException ex)
                {
                    Logger.getLogger(JavaFXApplication22.class.getName()).log(Level.SEVERE, null, ex);
                }

                ViewReceipt();
            }
        });

        VBox root = new VBox();
        root.getChildren().addAll(textArea, btn);

        Scene scene = new Scene(root, 500, 500);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void ViewReceipt()
    {
        try
        {
            Scanner s = new Scanner(new File("receipt.txt"));
            while (s.hasNext())
            {
                String line = s.nextLine();
                if (line.contains("$"))
                {
                    String[] splitLine = line.split("\s+");
                    for (String entry : splitLine)
                    {
                        if (entry.contains("$"))
                        {
                            textArea.appendText("\t\t\t\t " + entry + "\n");
                        }
                        else
                        {
                            textArea.appendText(entry + " ");
                        }
                    }
                }
                else if (line.contains("-"))
                {
                    textArea.appendText("\t\t\t\t\t\t" + line.trim() + "\n");
                    System.out.println(line);
                }
                else if (line.contains("Amount"))
                {
                    String[] splitLine = line.split("\s+");
                    textArea.appendText(splitLine[0] + "\t\t\t" + splitLine[1] + "\t\t" + splitLine[2] + "\n");
                }
                else
                {

                    textArea.appendText(line + "\n");
                }
            }
        }
        catch (FileNotFoundException ex)
        {
            System.err.println(ex);
        }
    }

    public String writeSpace(int length, String string)
    {
        String spaces = "";

        for (int i = 0; i < length - string.length(); i++)
        {
            spaces += " ";
        }

        System.out.println(string.length() + spaces.length());
        return string + spaces;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

Updated code!