public class AddDetails extends Application {
private final String FileName = "C:\\Users\\marsh\\OneDrive\\Documents\\CustomPrograms\\CalcProb\\Players.txt";
private String Name;
private char Hand1, Hand1;
private double Skill1, Skill2;
File file = new File(FileName);
FileOutputStream fos = null;
Writer writer = null;
static Stage classStage = new Stage();
String x = null;
public AddDetails() {
Name = "";
Hand = '\0';
Skill = 0.0;
BHand = '\0';
BSkill = 0.0;
}
@SuppressWarnings("restriction")
@Override
public void start(Stage myStage) throws IOException {
classStage = myStage;
myStage.setTitle("Details");
GridPane rootNode = new GridPane();
rootNode.setPadding(new Insets(15));
rootNode.setHgap(5);
rootNode.setVgap(5);
rootNode.setAlignment(Pos.CENTER);
Scene myScene = new Scene(rootNode, 300, 200);
rootNode.add(new Label("Name:"), 0, 0);
TextField name = new TextField();
rootNode.add(name, 1, 0);
rootNode.add(new Label("Hand:"), 0, 1);
TextField hand = new TextField();
rootNode.add(hand, 1, 1);
rootNode.add(new Label("Skill:"), 0, 2);
TextField skill = new TextField();
rootNode.add(skill, 1, 2);
rootNode.add(new Label("Skill:"), 0, 3);
TextField Bskill = new TextField();
rootNode.add(Bskill, 1, 3);
rootNode.add(new Label("Hand:"), 0, 4);
TextField Bhand = new TextField();
rootNode.add(Bhand, 1, 4);
Button SButton = new Button("Store");
rootNode.add(SButton, 1, 5);
GridPane.setHalignment(SButton, HPos.LEFT);
Button EButton = new Button("Finish");
rootNode.add(EButton, 1, 5);
GridPane.setHalignment(EButton, HPos.RIGHT);
name.setPromptText("Enter Name");
hand.setPromptText("Enter Hand);
skill.setPromptText("Enter Skill");
Bhand.setPromptText("Enter Hand");
Bskill.setPromptText("Enter Skill");
myStage.setScene(myScene);
myStage.show();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
fos = new FileOutputStream(file, true);
writer = new OutputStreamWriter(fos, "UTF-8");
EButton.setOnAction(e -> {
myStage.close();
try {
writer.close();
} catch (IOException writerclose) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Error Encountered");
alert.setContentText("Error: " + writerclose.getMessage());
}
});
SButton.setOnAction(e -> {
Name = name.getText();
Hand = hand.getText().charAt(0);
Skill = Double.valueOf(skill.getText());
BSkill = Double.valueOf(Bskill.getText());
BHand = Bhand.getText().charAt(0);
Hand = Character.toUpperCase(Hand);
BHand = Character.toUpperCase(BHand);
System.out.println(Name + "\t" + Hand + "\t" + Skill + "\t" + BHand + "\t" + BSkill);
try {
writer.write(Name + "\t" + Hand + "\t" + Skill + "\t" + BHand + "\t" + BSkill);
name.clear();
hand.clear();
skill.clear();
Bskill.clear();
Bhand.clear();
} catch (IOException br) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Error Encountered");
alert.setContentText("Error: " + br.getMessage());
}
});
} catch (IOException e) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Error Encountered");
alert.setContentText("Error: " + e.getMessage());
} finally {
try {
if (writer != null)
writer.close();
if (fos != null)
fos.close();
} catch (IOException finalclose) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Error Encountered");
alert.setContentText("Error: " + finalclose.getMessage());
}
}
}
public static void main(String[] Args) {
launch(Args);
}
}
Q
作家不寫任何文件
-3
A
回答
0
你正在關閉你的輸出文件,打開後立即沒有寫任何東西。
調用start
時,主線程中的打開和關閉運行,而點擊SButton
時嘗試寫入發生在事件線程中。
一個簡單的修復方法是打開和關閉SButton
動作偵聽器中的fos
進行寫操作。
0
這是代碼的一個選項。
我認爲你不需要是Writer類作爲一個全局變量。 只要你想修改你的文件,你就可以每次打開和寫入你的內容。
我只是修復了一些錯誤並使其運行。
SButton.setOnAction(e -> {
//.... skip
try {
fos = new FileOutputStream(file, true);
writer = new OutputStreamWriter(fos, "UTF-8");
writer.write(Name + "\t" + Hand + "\t" + Skill + "\t" + BHand + "\t" + BSkill);
name.clear();
hand.clear();
skill.clear();
Bskill.clear();
Bhand.clear();
writer.flush();
} catch (IOException br) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Error Encountered");
alert.setContentText("Error: " + br.getMessage());
}
finally {
try {
if (writer != null)
writer.close();
if (fos != null)
fos.close();
} catch (IOException finalclose) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Error Encountered");
alert.setContentText("Error: " + finalclose.getMessage());
}
}
});
所以,完整的源代碼是在這裏
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class AddDetails extends Application {
private final String FileName = "C:\\Users\\marsh\\OneDrive\\Documents\\CustomPrograms\\CalcProb\\Players.txt";
private String Name;
private char Hand, BHand;
private double Skill, BSkill;
private final File file = new File(FileName);
static Stage classStage = new Stage();
String x = null;
public AddDetails() {
Name = "";
Hand = '\0';
Skill = 0.0;
BHand = '\0';
BSkill = 0.0;
}
@SuppressWarnings("restriction")
@Override
public void start(Stage myStage) throws IOException {
classStage = myStage;
myStage.setTitle("Details");
GridPane rootNode = new GridPane();
rootNode.setPadding(new Insets(15));
rootNode.setHgap(5);
rootNode.setVgap(5);
rootNode.setAlignment(Pos.CENTER);
Scene myScene = new Scene(rootNode, 300, 200);
rootNode.add(new Label("Name:"), 0, 0);
TextField name = new TextField();
rootNode.add(name, 1, 0);
rootNode.add(new Label("Hand:"), 0, 1);
TextField hand = new TextField();
rootNode.add(hand, 1, 1);
rootNode.add(new Label("Skill:"), 0, 2);
TextField skill = new TextField();
rootNode.add(skill, 1, 2);
rootNode.add(new Label("Skill:"), 0, 3);
TextField Bskill = new TextField();
rootNode.add(Bskill, 1, 3);
rootNode.add(new Label("Hand:"), 0, 4);
TextField Bhand = new TextField();
rootNode.add(Bhand, 1, 4);
Button SButton = new Button("Store");
rootNode.add(SButton, 1, 5);
GridPane.setHalignment(SButton, HPos.LEFT);
Button EButton = new Button("Finish");
rootNode.add(EButton, 1, 5);
GridPane.setHalignment(EButton, HPos.RIGHT);
name.setPromptText("Enter Name");
hand.setPromptText("Enter Hand ");
skill.setPromptText("Enter Skill");
Bhand.setPromptText("Enter Hand");
Bskill.setPromptText("Enter Skill");
myStage.setScene(myScene);
myStage.show();
EButton.setOnAction(e -> {
myStage.close();
});
SButton.setOnAction(e -> {
Name = name.getText();
Hand = hand.getText().charAt(0);
Skill = Double.valueOf(skill.getText());
BSkill = Double.valueOf(Bskill.getText());
BHand = Bhand.getText().charAt(0);
Hand = Character.toUpperCase(Hand);
BHand = Character.toUpperCase(BHand);
FileOutputStream fos = null;
Writer writer = null;
System.out.println(Name + "\t" + Hand + "\t" + Skill + "\t" + BHand + "\t" + BSkill);
try {
fos = new FileOutputStream(file, true);
writer = new OutputStreamWriter(fos, "UTF-8");
writer.write(Name + "\t" + Hand + "\t" + Skill + "\t" + BHand + "\t" + BSkill);
name.clear();
hand.clear();
skill.clear();
Bskill.clear();
Bhand.clear();
writer.flush();
} catch (IOException br) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Error Encountered");
alert.setContentText("Error: " + br.getMessage());
}
finally {
try {
if (writer != null)
writer.close();
if (fos != null)
fos.close();
} catch (IOException finalclose) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Error Encountered");
alert.setContentText("Error: " + finalclose.getMessage());
}
}
});
}
public static void main(String[] Args) {
launch(Args);
}
}
輸入變量是下面的圖片,
保存的文件看起來像如下,
Regards,
相關問題
- 1. PHPExcel不寫任何文件
- 2. 緩衝作家不寫文本?
- 3. 的BufferedWriter不寫,甚至沖洗後作家爲.txt文件
- 4. PHPUnit的作曲家找不到文件
- 5. 作曲家找不到配置文件
- 6. 成功的寫並不會在文件上寫任何東西
- 7. 作曲家Paysafe任何版本?
- 8. 即使文件存在,作曲家也找不到composer.json文件
- 9. htaccess文件與重寫在任何域上工作
- 10. 參與linux中任何文件讀/寫操作的步驟
- 11. Python的CSV作家 - 寫在新的CSV文件列高達CSV文件
- 12. 如何實現獨家寫作,但非獨家閱讀?
- 13. 作曲家錯誤無法打開輸入文件:作曲家
- 14. 找不到任何文件
- 15. Magento2作曲家安裝不是從鎖文件 工作
- 16. 將函數寫入文件,得不到任何
- 17. 這段代碼爲什麼不寫任何東西到文件
- 18. Python腳本創建一個文件,但不寫任何東西
- 19. Windows 8商店應用程序XmlWriter不會寫任何文件
- 20. fputcsv不寫入CSV文件中的任何數據
- 21. 寫作HQL文件
- 22. 是否可以使用PLINQ寫入XmlWriter?任何其他流/作家?
- 23. 不寫文件
- 24. 耙文件任務不起作用
- 25. 任何替代方式寫入文件以外的文件
- 26. openoffice編寫器java sdk替換作家文檔中的文本
- 27. .htaccess文件重新寫不起作用
- 28. MappedByteBuffer寫入文件不起作用
- 29. 在Applescript中寫入文件 - 不工作
- 30. 寫作文件而不溢出
首先你的代碼格式不正確,其次,創建一個最小的,完整的和可驗證的例子:https://stackoverflow.com/help/mcve – Lichtbringer