首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

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

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

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

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文件相对应的格式

热点排行
Bad Request.