java 文件保存模拟

毛员外 2009-11-09 11:20:27
一个文本输入对话框:单行文本
一个保存按钮

效果:在文本输入对话框中输入一字符串,按保存按钮,字符串存入一txt文件中,文件名是输入的字符串

请给我一个完整的程序,我想多看看程序!api看了,但是不懂,很抽象
...全文
95 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
goodmrning 2009-11-10
  • 打赏
  • 举报
回复
up!
whut0802 2009-11-10
  • 打赏
  • 举报
回复
写的粗糙了点,LZ慢慢看吧,在自己的Eclipse中跑一下,导入相应的类库


public class Test extends JFrame implements ActionListener {
JButton saveBu=new JButton("保存");
JTextField jt=new JTextField(20);
JPanel jp=new JPanel();
//构造函数
public Test(){
setTitle("测试");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
saveBu.addActionListener(this);
jp.add(jt);
jp.add(saveBu);
add(jp);
setSize(600, 200);
setLocation(100, 100);
setVisible(true);
}
//事件处理
public void actionPerformed(ActionEvent e) {
String str=jt.getText();//获取文本框中的字符串
File file=new File(str+".txt");
System.out.println(file.getAbsolutePath());
FileWriter fw=null;
try {
fw=new FileWriter(file);
fw.write(str);
} catch (IOException e1) {
e1.printStackTrace();
} finally{
try {
fw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
//程序入口
public static void main(String[] args){
new Test();
}
}
快乐的2 2009-11-10
  • 打赏
  • 举报
回复
String fname=tf.getText();
File file = new File(fname+".txt");
if(!file.exists())
file.createNewFile();
PrintWriter pw = new PrintWriter(new FileOutputStream(file));
pw.print(fname);
pw.close();

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧