java 如何用JFileChooser保存文件
就是一个保存功能,把当前JTextArea存为txt文件,下面这个怎么改??
baocun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TXT","txt");//建立过滤器
chooser.setFileFilter(filter);//开始过滤
int returnVal =chooser.showSaveDialog(chooser);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
if (file.exists()) {
int copy = JOptionPane.showConfirmDialog(null,"是否要覆盖当前文件?", "保存", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if (copy == JOptionPane.YES_OPTION){
chooser.approveSelection();
}
}
else
chooser.approveSelection();
}
}
});
保存文件,其实就是IO的操作,将内存中的数据写入本地磁盘。可以用字陆扒梁节流OutputStream或是字符流Writer来完成。如果你写的是此如文本,建议用BufferedWriter,并且一行一行的写入。如果流早运的操作不清楚的,再问我
FileOutputStream out = new FileOutputStream(file);
out.write(textArea.getText().getBytes());
out.close();