String fileName="NoName.txt"; else if(eventSource==saveItem){
fileName="NoName.txt";
writeFile(fileName);
}
else if(eventSource==saveAsItem){
fileName=null;
writeFile(fileName);public void writeFile(String name){
File filea ;
String messagef="文件未找到";
FileOutputStream outputfile=null;
if(fileName==null){
JFileChooser savefile=new JFileChooser();
savefile.setDialogTitle("保存文件");
savefile.setApproveButtonText("保存");
savefile.showSaveDialog(this);
filea=savefile.getSelectedFile();
if(filea==null)return;
}
else {
filea=new File(fileName);
}
try{
outputfile=new FileOutputStream(filea);
}
catch(FileNotFoundException e){
JOptionPane.showMessageDialog(this, messagef, title, type);
}
String filecontent=textArea.getText();
String messaget="下文件错误";
try{
outputfile.write(filecontent.getBytes());
}
catch(IOException e){
JOptionPane.showMessageDialog(this, messaget, title, type);
}
String messagee="关闭流发生错误";
try{
outputfile.close();
}
catch(IOException e){
JOptionPane.showMessageDialog(this, messagee, title, type);
}
}
这些是关于 保存功能的。 只能另存可以用, 点保存没任何效果。。各位前辈可以帮看下吗?
filea=new File(fileName);
}
如果找不到NoName.txt,可能是保存没有加侦听器?你又没发那部分代码~不清楚
else if(eventSource==saveItem){
file|Name=writeFile(fileName);
}
else if(eventSource==saveAsItem){
fileName=null;
writeFile(fileName);
public String writeFile(String name){
File filea ;
String messagef="文件未找到";
FileOutputStream outputfile=null;
if(fileName==null){
JFileChooser savefile=new JFileChooser();
savefile.setDialogTitle("保存文件");
savefile.setApproveButtonText("保存");
savefile.showSaveDialog(this);
filea=savefile.getSelectedFile();
if(filea==null)return null;
}
else {
filea=new File(fileName);
}
try{
outputfile=new FileOutputStream(filea);
}
catch(FileNotFoundException e){
JOptionPane.showMessageDialog(this, messagef, title, type);
}
String filecontent=textArea.getText();
String messaget="下文件错误";
try{
outputfile.write(filecontent.getBytes());
}
catch(IOException e){
JOptionPane.showMessageDialog(this, messaget, title, type);
}
String messagee="关闭流发生错误";
try{
outputfile.close();
}
catch(IOException e){
JOptionPane.showMessageDialog(this, messagee, title, type);
}
return filea.getPath();
}
不知道为什么打多了个“|”
应为fileName=writeFile(fileName);