java保存和打开文件的方法
importjavax.swing.*;importjava.awt.event.*;classMyNotePad{JFramejf;JTextAreajta;JScro...
import javax.swing.*;
import java.awt.event.*;
class MyNotePad
{
JFrame jf;
JTextArea jta;
JScrollPane jsp;
JMenuBar jmb;
JMenu jm1,jm2,jm3;
JMenuItem jmi1,jmi2,jmi3,jmi4,jmi5,jmi6,jmi7,jmi8;
public MyNotePad(){
jf=new JFrame("记事本");
jf.setSize(400,400);
jta=new JTextArea("",20,40);
jsp=new JScrollPane(jta);
jmb=new JMenuBar();
jm1=new JMenu("文件");
jm2=new JMenu("编辑");
jm3=new JMenu("帮助");
jmi1=new JMenuItem("新建");
jmi2=new JMenuItem("退出");
jmi4=new JMenuItem("保存");
jmi8=new JMenuItem("打开");
jmi5=new JMenuItem("复制");
jmi6=new JMenuItem("粘贴");
jmi7=new JMenuItem("剪切");
jmi3=new JMenuItem("关于");
jmi1.addActionListener(new newl());
jmi2.addActionListener(new exit());
jmi3.addActionListener(new about());
jmi4.addActionListener(new save());
jmi5.addActionListener(new copy1());
jmi6.addActionListener(new paste1());
jmi7.addActionListener(new cut1());
jmi8.addActionListener(new open());
jm1.add(jmi1);
jm1.add(jmi2);
jm1.add(jmi4);
jm1.add(jmi8);
jm2.add(jmi5);
jm2.add(jmi6);
jm2.add(jmi7);
jm3.add(jmi3);
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm3);
//jd.add(jtb);
//jf.add(jd);
jf.
jf.getContentPane().add(jsp);
jf.setJMenuBar(jmb);
jf.setVisible(true);
}
class open implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser jfc1=new JFileChooser();
jfc1.showOpenDialog(jf);
open();
}
}
}
}
}
class save implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser jfc2 = new JFileChooser();
jfc2.showSaveDialog(jf);
save();
}
}
class copy1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.copy();
}
}
class paste1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.paste();
}
}
}
}
public void open(){}
public void save()
{
}
public static void main(String[] args){
new MyNotePad();
}
}
--------------------------
代码都发不上来,太长了,这样差不多也能看懂。
----------------------------
上面是我写的一个简单记事本,还没写好,
要调用showsavedialog和showopendialog,可是这样怎么获取路径来保存和打开呢?
也就是我的open()和save()怎么定义呢?
谢谢。
只有这些分了,55. 展开
import java.awt.event.*;
class MyNotePad
{
JFrame jf;
JTextArea jta;
JScrollPane jsp;
JMenuBar jmb;
JMenu jm1,jm2,jm3;
JMenuItem jmi1,jmi2,jmi3,jmi4,jmi5,jmi6,jmi7,jmi8;
public MyNotePad(){
jf=new JFrame("记事本");
jf.setSize(400,400);
jta=new JTextArea("",20,40);
jsp=new JScrollPane(jta);
jmb=new JMenuBar();
jm1=new JMenu("文件");
jm2=new JMenu("编辑");
jm3=new JMenu("帮助");
jmi1=new JMenuItem("新建");
jmi2=new JMenuItem("退出");
jmi4=new JMenuItem("保存");
jmi8=new JMenuItem("打开");
jmi5=new JMenuItem("复制");
jmi6=new JMenuItem("粘贴");
jmi7=new JMenuItem("剪切");
jmi3=new JMenuItem("关于");
jmi1.addActionListener(new newl());
jmi2.addActionListener(new exit());
jmi3.addActionListener(new about());
jmi4.addActionListener(new save());
jmi5.addActionListener(new copy1());
jmi6.addActionListener(new paste1());
jmi7.addActionListener(new cut1());
jmi8.addActionListener(new open());
jm1.add(jmi1);
jm1.add(jmi2);
jm1.add(jmi4);
jm1.add(jmi8);
jm2.add(jmi5);
jm2.add(jmi6);
jm2.add(jmi7);
jm3.add(jmi3);
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm3);
//jd.add(jtb);
//jf.add(jd);
jf.
jf.getContentPane().add(jsp);
jf.setJMenuBar(jmb);
jf.setVisible(true);
}
class open implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser jfc1=new JFileChooser();
jfc1.showOpenDialog(jf);
open();
}
}
}
}
}
class save implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser jfc2 = new JFileChooser();
jfc2.showSaveDialog(jf);
save();
}
}
class copy1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.copy();
}
}
class paste1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.paste();
}
}
}
}
public void open(){}
public void save()
{
}
public static void main(String[] args){
new MyNotePad();
}
}
--------------------------
代码都发不上来,太长了,这样差不多也能看懂。
----------------------------
上面是我写的一个简单记事本,还没写好,
要调用showsavedialog和showopendialog,可是这样怎么获取路径来保存和打开呢?
也就是我的open()和save()怎么定义呢?
谢谢。
只有这些分了,55. 展开
1个回答
展开全部
可以不用那个方法,这样就能用io里的fileinputstream()来作了,是吧?
public void readFile()//用于读取文件内容
{
try
{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str;
while((str = br.readLine()) != null)
{
txtEdit.setText(txtEdit.getText()+str+"\n");
}
br.close();
fr.close();
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
public void saveFile()//保存文件
{
try
{
FileWriter fw = new FileWriter(file);
fw.write(txtEdit.getText());
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
我大致知道你的意思应该怎么作了,你给弹出的button++事件就好了,代码不用说了吧,你自己想想吧!
我用给你作了下,
给你点代码:
public void open()throws IOException{
String filepath=jfc1.getSelectedFile().getAbsolutePath();
FileReader fr=new FileReader(filepath);
BufferedReader br=new BufferedReader(fr);
String str;
while((str = br.readLine())!=null)
jta.setText(jta.getText()+str+"\n");
br.close();
fr.close();
}
public void save()throws IOException{
String filepath2=jfc2.getSelectedFile().getAbsolutePath();
FileWriter fw=new FileWriter(filepath2);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw);
pw.print(jta.getText());
bw.close();
fw.close();
}
public void readFile()//用于读取文件内容
{
try
{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String str;
while((str = br.readLine()) != null)
{
txtEdit.setText(txtEdit.getText()+str+"\n");
}
br.close();
fr.close();
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
public void saveFile()//保存文件
{
try
{
FileWriter fw = new FileWriter(file);
fw.write(txtEdit.getText());
fw.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
我大致知道你的意思应该怎么作了,你给弹出的button++事件就好了,代码不用说了吧,你自己想想吧!
我用给你作了下,
给你点代码:
public void open()throws IOException{
String filepath=jfc1.getSelectedFile().getAbsolutePath();
FileReader fr=new FileReader(filepath);
BufferedReader br=new BufferedReader(fr);
String str;
while((str = br.readLine())!=null)
jta.setText(jta.getText()+str+"\n");
br.close();
fr.close();
}
public void save()throws IOException{
String filepath2=jfc2.getSelectedFile().getAbsolutePath();
FileWriter fw=new FileWriter(filepath2);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw);
pw.print(jta.getText());
bw.close();
fw.close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询