文件保存的问题(紧急!)进来看看啊! 我想在JTxteArea中编辑文件,通过文件保存对话框保存,请教如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TestFile extends JFrame implements ActionListener{ JTextArea jta = new JTextArea(); JButton jb = new JButton("保存"); public TestFile(){ Container container = this.getContentPane(); container.add(jta); container.add(jb,"South"); jb.addActionListener(this); this.setLocation(200,200); this.setSize(300,400); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e){ String content = jta.getText(); java.awt.FileDialog fd = new FileDialog(this,"save",FileDialog.SAVE); fd.setVisible(true); String name = fd.getFile(); String directory = fd.getDirectory(); String savePath = directory + name + ".txt"; System.out.println (savePath); try{ FileWriter fw = new FileWriter(savePath); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); fw.close(); }catch(Exception e1){ e1.printStackTrace(); } } public static void main(String[] args){ new TestFile(); }} 注册个 SDN 成员,订阅他的 Java Newsletter 周刊。developer.sun.com 每个星期给你一篇文章,一般每篇文章讲到 3- 6 个知识点。你也可以到邮件中提到的站点上搜索 以前的每期文章。 我又出错了,求大虾指导!!!!!!!!!!! 弹出式菜单出问题了 上传问题?? 搞定100分 Struts 的web.xml配置问题 为什么Class.forName("..")找不到class? 哪里有Thinking in java第二版的英文版下载呀? 如何定位到文件的最后,进行文件追加 有关内部类的疑问! 小弟有一关于weblogic5.1下使用jdbc的问题向大家请教:...(答者有分) 关于GUI中窗口之间信息传递的问题 大哥,帮我看看这个方法 请高手指点
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TestFile extends JFrame implements ActionListener{
JTextArea jta = new JTextArea();
JButton jb = new JButton("保存");
public TestFile(){
Container container = this.getContentPane();
container.add(jta);
container.add(jb,"South");
jb.addActionListener(this);
this.setLocation(200,200);
this.setSize(300,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
String content = jta.getText();
java.awt.FileDialog fd = new FileDialog(this,"save",FileDialog.SAVE);
fd.setVisible(true);
String name = fd.getFile();
String directory = fd.getDirectory();
String savePath = directory + name + ".txt";
System.out.println (savePath);
try{
FileWriter fw = new FileWriter(savePath);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
fw.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
public static void main(String[] args){
new TestFile();
}
}