0
我遇到了問題我無法在使用NetBeans的file.txt
中寫入任何內容。我不知道爲什麼我嘗試了很多,但它不起作用。用文本文件寫入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);
}}
這是一個遊戲,玩家要創造一個新的孔特在這裏打球是我的類控制器
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"
上的文本字段中有什麼,但它沒有寫出來,我真的需要幫助,因爲我搜索了很多,但沒有找到任何解決方法可以解決問題。
我可以問一個[mcve]嗎? –
嘗試if(file.exists()){...} else {//創建文件並寫入它} < - 只是一個猜測。 – Sedrick