How to use the JFileChooser Showsavedialog () method--to implement the Save File feature.
JFileChooser's Showsavedialog () method, you can open a Save File dialog box, how to achieve the specific-save the file function.
Basic idea: Open File-----Get file properties [File path + filename]----> read from disk using input stream [InputStream]
Save File-----> Create file properties [file path + filename]----> write to disk using output stream [OutputStream]
When you use the Swing JFileChooser Showsavedialog () method to open a file dialog to save it, you must be aware of a few things:
1, Chooser.getselectedfile (); This function: if the Showopendialog () dialog box, returned is the dialog box selected files;
If the dialog type is Showsavedialog, the value returned here is the file you want to save, the file may exist, and may not exist. If it does not exist, return the name of the file you entered in the dialog box.
Now that you know the file, if it does not exist, create a new one, and then write the data to the file, you can save the implementation. In addition JFileChooser will not automatically help you read the data and go in, these are to be implemented by the code.
2, the implementation of the complete function is as follows:
The open and save are two JButton, implement the function of the button----open and save the file, the implementation of the code is as follows [anonymous class registration of listening events]
//.... Implementation--"File open" ... "File Save". Function ... ....... ........ ..... ...... ..... ............... ....... .......
Open.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent e) {
Filechooser.showopendialog (Mymenueditor.this); JFileChooser Filechooser
BufferedReader BR =null;
Contentarea.settext (""); Contentarea is an edit text area JTextArea
try {
br = new BufferedReader (new InputStreamReader (New FileInputStream (Filechooser.getselectedfile)));
while (true) {
String content = Br.readline ();//read one line at a time
if (content==null) break;
Contentarea.append (content);
Contentarea.append ("\ n");/linefeed
}
catch (FileNotFoundException E1) {
Joptionpane.showmessagedialog (mymenueditor.this, "File not found-open failed");
catch (IOException E1) {
Joptionpane.showmessagedialog (mymenueditor.this, "file read exception");
}finally{
try {
if (br!=null) br.close ();
catch (IOException E1) {
}
}
}
});
Save.addactionlistener (new ActionListener () {
BufferedWriter bw = NULL;
@Override
public void actionperformed (ActionEvent e) {
int select = Filechooser.showsavedialog (mymenueditor.this);
Filechooser.setselectedfile New File ("New. txt");
File file = null;
String fileName = null;
if (select==jfilechooser.approve_option) {
File =filechooser.getselectedfile (); If any of the files are not selected here, the following filechooser.getname (file) will return the file name entered by the hand
}
FileName = filechooser.getname (file);
if (filename==null| | Filename.trim (). Length () ==0) {
Joptionpane.showmessagedialog (mymenueditor.this, "file name is empty.) ");
}
if (File.isfile ()) {
FileName = File.getname ();
}
Otherwise, it's a folder.
File = Filechooser.getcurrentdirectory ();//Get current directory
String path = File.getpath () +java.io.file.separator+filename;
File =new file (path);
if (file.exists ()) {//If you select an existing file----ask if you want to overwrite
int i = Joptionpane.showconfirmdialog (Mymenueditor.this, "The file already exists, are you sure you want to overwrite it?") ");
if (i = = joptionpane.yes_option);
else return;
}
try {
BW = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (file));
Bw.write (Contentarea.gettext ());
Bw.flush ();
catch (FileNotFoundException E1) {
Joptionpane.showmessagedialog (mymenueditor.this, "File Save Error" +e1.getmessage ());
catch (IOException E1) {
E1.printstacktrace ();
}finally{
try {
if (bw!=null) bw.close ();
catch (IOException E1) {
}
}
}
});