2017-05-12 6 views
0

私は問題があります。私はfile.txtにnetbeansを使って何も書き込めません。なぜ私はたくさん試したのか分かりませんが、うまくいきません。テキストファイルに書きます。javafx netbeans

これは私のコードです:

public class pendu extends Application { 


@Override 
public void start(Stage stage) throws Exception { 
    Parent root = FXMLLoader.load(getClass().getResource("connect.fxml")); 

    Scene scene = new Scene (root); 
    stage.setScene(scene); 
    stage.show(); 

} 


/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) throws IOException { 
    launch(args); 

}} 

これはゲームで、ゲーマーがここに再生するための新しいcompteは私のクラスのコントローラ

public class ConnectController implements Initializable { 
@FXML 
private Button new_gamer; 

@FXML 
private Button old; 

@FXML 
private Text title; 

@FXML 
private TextField pseudo; 
public void new_compte(ActionEvent event) throws IOException 
{ 
    Gamer joueur = new Gamer(); 
    if(joueur.firstletter(pseudo.getText())) 
    { 

     /*if(joueur.existPseudo(pseudo.getText())) 
     { 
      Alert alert = new Alert(AlertType.INFORMATION); 
      alert.setTitle("Erreur"); 
      alert.setHeaderText("Ce pseudo existe déja"); 
      alert.setContentText("veuillez changer votre pseudo"); 
     } 
     else 
     {*/ 

      joueur.saveGamer(pseudo.getText()); 
     // } 
    } 
    else 
    { 
     Alert alert = new Alert(AlertType.INFORMATION); 
     alert.setTitle("Erreur"); 
     alert.setHeaderText("votre pseudo ne commence pas par une lettre"); 
     alert.setContentText("veuillez changer votre pseudo"); 

     alert.showAndWait(); 
    } 

} 


@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
}  

private static class JFXTextField { 

    public JFXTextField() { 
    } 

    private String getText() { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 
} 

、ここでクラスのゲーマー

のコードでレコード生成すべきです
public class Gamer 
{ 
private String pseudo; 
private int bestScore; 
private int currentScor; 
public Gamer (String pseu,int bestScore,int currentScore) 
{ 
    this.pseudo = pseu; 
    this.bestScore = bestScore; 
    this.currentScor = currentScore; 
} 
public Gamer() 
{ 


} 
public void setPseudo(String pseudo) 
{ 
    this.pseudo = pseudo; 
} 
public static void saveGamer(String pseu) throws FileNotFoundException, IOException 
{ 
File file = new File("Joueurs.txt"); 
if (file.exists()) 
{ 
    FileWriter fileWriter = new FileWriter(file,true); 
BufferedWriter fileOut = new BufferedWriter(fileWriter);//fileWriter 
    fileOut.write(pseu); 
fileOut.newLine(); 
    fileOut.close(); 
fileWriter.close(); 
} 
} 

public boolean firstletter(String pseudo) 
{ 
    return(Character.isLetter(pseudo.charAt(0))); 
} 
public boolean existPseudo(String pseudo) throws FileNotFoundException, IOException 
{ 

    InputStream flux =new FileInputStream("joueurs.txt"); 

    InputStreamReader lecture=new InputStreamReader(flux); 
    BufferedReader buff=new BufferedReader(lecture); 
    String ligne; 
    ArrayList<String> joueurs =new ArrayList<String>(); 
    while ((ligne=buff.readLine())!=null) 
    { 
     String[] tabChaines = ligne.split(";"); 

     joueurs.add(tabChaines[0]); 

    } 
    Collections.sort(joueurs); 
    return(Collections.binarySearch(joueurs,pseudo)>=0); 

} 
public void afficher() 
{ 

} 
public void setBestScr(int newScor) 
{ 
    bestScore = (newScor < bestScore) ? bestScore : newScor; 
}} 

コンセプトは、ボタンnew_compteをクリックしたときに書き込む必要がありますテキストファイル"joueurs.txt"のテキストフィールドには何が書かれていないのですが、多くを検索したので本当に助けが必要ですが、問題を解決できる解決策は見つかりませんでした。

+1

[mcve]をお尋ねしますか? –

+1

if(file.exists()){...} else {//ファイルを作成して書き込みます} < - まあまあです。 – Sedrick

答えて

0

ファイルが存在しない場合、アプリケーションは新しいファイルを作成しません。 if(file.exists())のif条件を削除できると思います。

関連する問題