帮忙解释一段代码,java的!急!!!

来源:百度知道 编辑:UC知道 时间:2024/07/02 06:15:49
这是一个保存文件的事件,是不是选择一个空文件来保存Jtextpane(ta)里的内容?保存时 file = chooser.getSelectedFile();是什么意思?
总之,希望最好能把每条代码都解释下,谢谢了!

if (e.getSource() == save) {
result = chooser.showSaveDialog(ta);
file = null;
if (result == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
la.setText("存储文件名为:" + file.getName());
} else if (result == JFileChooser.CANCEL_OPTION) {
la.setText("您没有选择任何文件");
}

FileOutputStream fileOutStream = null;

if (file != null) {
try {
fileOutStream = new FileOutputStream(file);
} catch (FileNotFoundException fe) {
la.setText("File Not Found");
return;

//检测事件源是不是save
if (e.getSource() == save) {
//文件选择器打开保存文件对话框,关闭后将返回一个int值来指示操作结果
result = chooser.showSaveDialog(ta);
file = null;

//检测是不是点击了确认按钮而关闭了文件选择器
if (result == JFileChooser.APPROVE_OPTION) {

//返回一个被选择器选中的文件对象
//(不是文件路径,而是文件对象,通过此对象方能取得文件路径)
file = chooser.getSelectedFile();

//la 的内容设置为 "存储文件名为:" + file.getName()
la.setText("存储文件名为:" + file.getName());

//如果点击了取消按钮而关闭了文件选择器
} else if (result == JFileChooser.CANCEL_OPTION) {

//la 的内容设置为 "您没有选择任何文件"
la.setText("您没有选择任何文件");
}

//声明文件输出流对象 fileOutStream
FileOutputStream fileOutStream = null;

if (file != null) {
try {

//以文件 file 新建文件输出流对象,
fileOutStream = new FileOutputStream(file);
} catch (FileNotFoundException fe) {
la.setText("File Not Found");
return;
}

//将ta中内容保存成一个String对象:content
Str