写入一个文本文件,然后读回到一个textarea javafx
我已经做了一个javafx应用程序,将一个订单写入一个txt文件,然后即时阅读txt文件到一个textarea。 我的代码正在工作并正在打印文件,但我不知道如何正确地格式化它。我是javafx的noob,我写错了吗?任何帮助表示赞赏写入一个文本文件,然后读回到一个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*********,
你可以玩这个。如果所有收据看起来都非常相似。
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);
}
}
更新代码!
我试过了你发布的代码,它完全打印到文本文件,但仍然有相同的格式问题读取到textarea。 –
我们可以使用另一个控件来代替textarea吗? –
你只需要玩数字。 – Sedrick
你为什么要使用.useDelimiter( “”);?我会从文件中读取一行,然后将该行附加到textArea。我不明白为什么它不会完全一样。删除.useDelimiter并更改appendText(s.next()); appendText(s.nextLine()): – Sedrick
我尝试过,但它在一行中打印所有内容 –
我相信它是我写入文件的方式,还有更好的方法吗? –