我在使用BufferedWriter从文本输入到JTextField中将文本写入文件时遇到问题。
问题出现在以下代码中的event.getSource == inputAdd
语句中。
public void actionPerformed(ActionEvent event){
Object source = event.getSource();
if (event.getSource() == inputOpen){
file.openFile(inputOpenFile.getText());
currentPlaylist = file.getFileName();
contentList.setText(file.fileRead()); //read file
contentTitle.setText("Current Movie Playlist: " + currentPlaylist); //set text to what's in file
}
if (event.getSource() == inputCreate){
file.createFile(inputCreateFile.getText());
currentPlaylist = file.getFileName();
inputCreateFile.setText(""); //set text to nothing
}
if (event.getSource() == inputAdd){
//file.openFile(inputOpenFile.getText());
//file.createElement(inputAddMovie.getText());
movie = inputAddMovie.getText();
try{
BufferedWriter writer = new BufferedWriter(new FileWriter(currentPlaylist));
writer.write(movie);
writer.newLine();
writer.close();
}catch(Exception error){
System.out.println("There was an error");
}
}
答案 0 :(得分:0)
这个问题很模糊,但是因为它在“inputAdd”中,我只是假设你要附加到文件,在这种情况下你的FileWriter应该是
BufferedWriter writer = new BufferedWriter(new FileWriter(currentPlaylist, true)); //notice the extra argument
同时将您的if语句更改为
if(source == input....)