I have a problem I can't write any thing in my file.txt
using netbeans. I don't know why I tried a lot, but it is not working.
This is my code:
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);
}}
this is a game and gamers should creat a new compte to play here is the code of my class controller
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.
}
}
and here class gamer
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;
}}
The concept is when I click on the button new_compte
, it should write what is in the textfield on the text file "joueurs.txt"
, but it's not writing it, and I really need help because I searched a lot, but I didn't find any solution which can fix the problem.
If the file doesn't existed, the application will not create new file. I think that you can remove the if condition if (file.exists()) for this case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.