【问题标题】:Write in a text file javafx netbeans写入文本文件 javafx netbeans
【发布时间】:2017-05-12 12:18:41
【问题描述】:

我有一个问题,我无法使用 netbeans 在我的 file.txt 中写任何东西。我不知道为什么我尝试了很多,但它不起作用。

这是我的代码:

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

}}

这是一个游戏,玩家应该创建一个新的游戏来玩这里是我的类控制器的代码

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"上写下文本字段中的内容,但不是写它,我真的需要帮助,因为我搜索了很多,但是我没有找到任何可以解决问题的解决方案。

【问题讨论】:

标签: java javafx netbeans file-io text-files


【解决方案1】:

如果文件不存在,应用程序将不会创建新文件。 我认为您可以在这种情况下删除 if 条件 if (file.exists())。

【讨论】:

    猜你喜欢
    • 2020-04-03
    • 2017-10-25
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    相关资源
    最近更新 更多