日期:2014-05-20  浏览次数:20789 次

如何实现点击保存对话框里的保存按钮就能实现保存文件
如何实现点击保存对话框里的保存按钮就能实现保存文件 

fc2.setCurrentDirectory(new File("C:/Documents and Settings/Administrator/桌面"));
int result=fc2.showSaveDialog(xuesheng.this);
 
int count;
byte input[]=new byte[256];
String name2;
BufferedInputStream bufin=new BufferedInputStream(System.in);
try{

count=bufin.read(input);
name2=new String(input);
File fp=new File(name2);
}
catch(IOException y )
{
System.out.println("IO错误");
}


这是保存部分的代码 我想在保存对话框里面保存文件 
名字是自己自定义的 文件格式是.txt的

求问各位大虾 我该怎么办

------解决方案--------------------
好象漏了点东西,接着你的代码
Java code

String path=null;
if(result==JFileChooser.APPROVE_OPTION)
{
    path=fc2.getSelectFile().getAbsulotePath();
    DataOutputStream dos=new DataOutputStream(new FileOutputStream(new File(path)));
    //下面才是保存数据的过程
}

------解决方案--------------------
fp没有"落地",再加一句
fp.createNewFile();
------解决方案--------------------
//..省略定义

MenuItem itemSave = new MenuItem("保存文件");
itemSave.addActionListener(this);
//..省略
FileDialog filedialog_save = new FileDialog(this, "保存", FileDialog.SAVE);
public void actionPerformed(ActionEvent e) 
{
//判断是否是【保存】
if (e.getSource() == itemSave) 
{
filedialog_save.setVisible(true);
if (filedialog_save.getFile() != null)
{
try {
File file = new File(filedialog_save.getDirectory(),
filedialog_save.getFile());
tofile = new FileWriter(file);
out = new BufferedWriter(tofile);
out.write(text.getText(), 0, (text.getText()).length());
out.close();
tofile.close();

catch (IOException e2) {}
}
}
}
------解决方案--------------------
像3楼那样写个弹出框就行
FileDialog filedialog_save = new FileDialog(this, "保存", FileDialog.SAVE); 
如果是J2EE中 可以设置content-type为txt文件相对应的格式