2017-09-10 8 views
-3
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); 
    } 
} 
+4

Premièrement, votre code n'est pas formaté correctement, deuxièmement, créez un exemple minimal, complet et vérifiable: https://stackoverflow.com/help/mcve – Lichtbringer

Répondre

0

Vous fermez votre fichier de sortie immédiatement après son ouverture, sans rien écrire.

L'exécution de l'ouverture et de la fermeture dans votre thread principal lorsque start est appelée, tandis que l'écriture tentée se produit dans le thread d'événement lorsque vous cliquez sur SButton.

Une solution simple serait d'ouvrir et de fermer fos dans le programme d'écoute d'action SButton qui effectue l'écriture.

0

Ceci est une option du code.

Je pense que vous n'avez pas besoin d'être la classe Writer en tant que variable globale. Vous venez d'ouvrir et d'écrire votre contenu à chaque fois que vous voulez modifier votre fichier. Je viens de corriger une erreur et l'ai fait fonctionner.

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

Ainsi, la source complète est ici

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

La variable d'entrée est l'image ci-dessous,

enter image description here

Le fichier enregistré ressemble comme suit,

enter image description here

Cordialement,