【发布时间】: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"上写下文本字段中的内容,但不是写它,我真的需要帮助,因为我搜索了很多,但是我没有找到任何可以解决问题的解决方案。
【问题讨论】:
-
我可以要一个minimal reproducible example吗?
-
试试 if(file.exists()){...}else{//create file and write to it}
标签: java javafx netbeans file-io text-files