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);
}
}
-3
A
답변
0
출력 파일을 열면 아무것도 쓰지 않고 닫습니다.
이 호출 될 때 주 스레드에서 열기 및 닫기가 실행되는 반면 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);
}
}
저장된 파일은 다음과 같습니다
,
감사합니다.
관련 문제
- 1. fwrite 함수가 파일에 아무 것도 쓰지 않습니다.
- 2. 성공적인 쓰기는 파일에 아무 것도 쓰지 않습니다.
- 3. FileChannel은 아무 것도 쓰지 않습니다.
- 4. 파이썬 로깅 모듈이 파일에 아무 것도 쓰지 않습니다.
- 5. Windows 8 store app XmlWriter가 파일에 아무 것도 쓰지 않습니다.
- 6. 문자열 작성자가 파일에 쓰지 않는다
- 7. PhpExcel setCellValue는 아무 것도 쓰지 않습니다.
- 8. php flush(); 페이지에 아무 것도 쓰지 않습니다.
- 9. TextIO.outputSubstr()은 아무 것도 쓰지 않습니다
- 10. 이 코드가 파일에 아무 것도 쓰지 않는 이유
- 11. 이 함수가 왜 내 파일에 Perl로 아무 것도 쓰지 않습니까?
- 12. 내 파일에 아무 것도 쓰지 않는 이유는 무엇입니까?
- 13. 파일에 복식 쓰기, 아무 것도 쓰지 않는 문제
- 14. 기존 파일에 아무 것도 쓰지 않고 fwrite()가 실패합니다.
- 15. java printWrite는 파일에 아무 것도 쓰지 않지만 파일은 생성됩니다.
- 16. 파이썬 스크립트는 파일을 만들지 만 아무 것도 쓰지 않습니다.
- 17. CrashReports.csv 파일에 ACRA에 아무 것도 표시되지 않습니다.
- 18. Python : 파일에 추가하려고하지만 아무 것도 쓰여지지 않습니다.
- 19. AJAX가 PHP 파일에 아무 것도 보내지 않습니다.
- 20. 작성자가 닫을 때 하위 디렉토리의 파일에 텍스트를 쓰지 않습니다.
- 21. FileOutputStream이 파일에 쓰지 않습니다.
- 22. Laravel - 테이블의 새 열에 아무 것도 쓰지 않습니다.
- 23. NHibernate에 대한 log4net은 ASP.NET 추적에 아무 것도 쓰지 않습니다.
- 24. DeflateStream CopyTo는 아무 것도 쓰지 않고 예외를 throw하지 않습니다.
- 25. 아무 것도 반환되지 않습니다.
- 26. BufferedWriter가 텍스트 파일에 쓰지 않습니다.
- 27. 이 코드는 텍스트 파일에 아무 것도 쓰지 않습니다 ?? ": \ 등급 C": C#을
- 28. ListView에서 아무 것도 클릭 할 때 아무 것도 발생하지 않습니다.
- 29. If 문에 아무 것도 추가하지 않으면 아무 것도 작동하지 않습니다.
- 30. MySql이 아무 것도 반환하지 않습니다.
먼저 코드의 형식이 올바르지 않습니다. 둘째, 최소한의 완전하고 검증 가능한 예를 만드십시오. https://stackoverflow.com/help/mcve – Lichtbringer