代码如下:
package NotePad;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class NotePad extends JFrame implements ActionListener{ public void actionPerformed(ActionEvent e) {
// 判断哪个菜单被按下
if(e.getActionCommand().equals("open"))
{
//隆重推荐JFileChooser
JFileChooser jfc1=new JFileChooser();
jfc1.setDialogTitle("请选择文件...");
jfc1.showOpenDialog(null);
jfc1.setVisible(true);
//知道用户选择哪一个文件(绝对路径)
String filename=jfc1.getSelectedFile().getAbsolutePath();
FileReader fr=null;
BufferedReader br=null;
try {
fr=new FileReader(filename);
br=new BufferedReader(fr);
//从文件中读取信息并显示到jta
String s="";
String allcon="";
while((s=br.readLine())!=null)
{
allcon+=s+"\r\n";
}
jta.setText(allcon);
} catch (Exception arg) {
arg.printStackTrace();// TODO: handle exception
}finally{
try {
br.close();
fr.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
if(e.getActionCommand().equals("save"))
{
JFileChooser jfc2=new JFileChooser();
jfc2.setDialogTitle("另存为...");
jfc2.showSaveDialog(null);
jfc2.setVisible(true);
//得到用户希望保存的路径
String filename2=jfc2.getSelectedFile().getAbsolutePath();
System.out.println(filename2);
FileWriter fw=null;
BufferedWriter bw=null;
try {
fw=new FileWriter(filename2);
bw=new BufferedWriter(fw);
bw.write(this.jta.getText());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
fw.close();
bw.close();
} catch (Exception e1) {
// TODO: handle exception
}
}
}
}
//定义需要的SWING组件
JTextArea jta=null;
JMenuBar jmb=null;
JMenu jm1=null;
JMenuItem jmi1=null;
JMenuItem jmi2=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
NotePad notepad=new NotePad();
//菜单条
}
public NotePad()
{
jta=new JTextArea();
jmb=new JMenuBar();
jm1=new JMenu("文件");
//设置助记符
jm1.setMnemonic('F');
//jmi1=new JMenuItem("打开",new ImageIcon("/caocao.jpg"));
jmi1=new JMenuItem("打开");
//注册监听
jmi1.addActionListener(this);
jmi1.setActionCommand("open");
jmi2=new JMenuItem("保存");
jmi2.addActionListener(this);
jmi2.setActionCommand("save");
//加入
this.setJMenuBar(jmb);
//把jm1放入到jmb
jmb.add(jm1);
//把item放入Menu
jm1.add(jmi1);
jm1.add(jmi2);
this.add(jta);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400,300);
this.setVisible(true);
}}
其中:
fw=new FileWriter(filename2);
bw=new BufferedWriter(fw);
bw.write(this.jta.getText());
保存到新的文件中没有数据(新文件为空)
谢谢!!!
package NotePad;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class NotePad extends JFrame implements ActionListener{ public void actionPerformed(ActionEvent e) {
// 判断哪个菜单被按下
if(e.getActionCommand().equals("open"))
{
//隆重推荐JFileChooser
JFileChooser jfc1=new JFileChooser();
jfc1.setDialogTitle("请选择文件...");
jfc1.showOpenDialog(null);
jfc1.setVisible(true);
//知道用户选择哪一个文件(绝对路径)
String filename=jfc1.getSelectedFile().getAbsolutePath();
FileReader fr=null;
BufferedReader br=null;
try {
fr=new FileReader(filename);
br=new BufferedReader(fr);
//从文件中读取信息并显示到jta
String s="";
String allcon="";
while((s=br.readLine())!=null)
{
allcon+=s+"\r\n";
}
jta.setText(allcon);
} catch (Exception arg) {
arg.printStackTrace();// TODO: handle exception
}finally{
try {
br.close();
fr.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
if(e.getActionCommand().equals("save"))
{
JFileChooser jfc2=new JFileChooser();
jfc2.setDialogTitle("另存为...");
jfc2.showSaveDialog(null);
jfc2.setVisible(true);
//得到用户希望保存的路径
String filename2=jfc2.getSelectedFile().getAbsolutePath();
System.out.println(filename2);
FileWriter fw=null;
BufferedWriter bw=null;
try {
fw=new FileWriter(filename2);
bw=new BufferedWriter(fw);
bw.write(this.jta.getText());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
fw.close();
bw.close();
} catch (Exception e1) {
// TODO: handle exception
}
}
}
}
//定义需要的SWING组件
JTextArea jta=null;
JMenuBar jmb=null;
JMenu jm1=null;
JMenuItem jmi1=null;
JMenuItem jmi2=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
NotePad notepad=new NotePad();
//菜单条
}
public NotePad()
{
jta=new JTextArea();
jmb=new JMenuBar();
jm1=new JMenu("文件");
//设置助记符
jm1.setMnemonic('F');
//jmi1=new JMenuItem("打开",new ImageIcon("/caocao.jpg"));
jmi1=new JMenuItem("打开");
//注册监听
jmi1.addActionListener(this);
jmi1.setActionCommand("open");
jmi2=new JMenuItem("保存");
jmi2.addActionListener(this);
jmi2.setActionCommand("save");
//加入
this.setJMenuBar(jmb);
//把jm1放入到jmb
jmb.add(jm1);
//把item放入Menu
jm1.add(jmi1);
jm1.add(jmi2);
this.add(jta);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400,300);
this.setVisible(true);
}}
其中:
fw=new FileWriter(filename2);
bw=new BufferedWriter(fw);
bw.write(this.jta.getText());
保存到新的文件中没有数据(新文件为空)
谢谢!!!
解决方案 »
- hibernate的缓存问题
- 初学servlet 看了韩顺平的视频, 写了简单用户登录的代码,继承了Httpservlet ,发现不能进行页面跳转,各位大神看看,小弟没分,真心求助
- mule axis 发布webservice后,如何获取客户端ip
- 哪位高手教下,简单的方法调用??
- 谁能帮我解决下这个问题,我谢谢他了!!
- 一个关于multibox的问题,分不够就加!!
- 分不式缓存问题,请给位留步
- 高分求JBoss下配置MS Sql Server数据库!!!
- 请教在如何使用OpenSSL或者是Keytool生成WebLogic的服务器证书?
- java排序id乱问题求大神解决爱你。
- Could not open JPA EntityManager for transaction
- 这段正则表达式不知道出了什么问题,没有用
bw.close();
加上这两句。
fw.close();
bw.close();应该为:
bw.close();fw.close();