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

时间:2017-04-01 17:14:00

标签: javafx scenebuilder

我已经创建了一个将订单写入txt文件的javafx应用程序,然后我将txt文件读入textarea。 我的代码正在运行并正在打印文件,但我不知道如何对其进行核心格式化。我是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            $0.00,
Sales Tax 13%           $0.00,
Total Sale              $0.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);
    }


}

这就是textarea中的情况

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

1 个答案:

答案 0 :(得分:0)

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

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);
    }

}
  

更新了代码!

enter image description here

enter image description here

相关问题