Java通过I/O简单记事本(暂时读写功能,等待完善)

10 篇文章 0 订阅
package txt;


/**
 * 记事本
 */
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.*;


import java.io.*;


public class Txt extends JFrame implements ActionListener {
private JScrollPane jScrollPane = new JScrollPane();
private JTextArea jTextArea = new JTextArea();
private JMenuBar jMenuBar = new JMenuBar();// 菜单栏
private JMenu jMenu = new JMenu("文件(F)");// 菜单
private JMenu jMenu2 = new JMenu("编辑(E)");
private JMenu jMenu3 = new JMenu("格式(O)");
private JMenu jMenu4 = new JMenu("查看(V)");
private JMenu jMenu5 = new JMenu("帮助(H)");
private JMenuItem jMenuItem = new JMenuItem("新建(N)");
private JMenuItem jMenuItem2 = new JMenuItem("打开(O)");
private JMenuItem jMenuItem3 = new JMenuItem("保存(S)");
private JMenuItem jMenuItem4 = new JMenuItem("退出(X)");
private JMenuItem jMenuItem21 = new JMenuItem("撤销(U)");
private JMenuItem jMenuItem22 = new JMenuItem("剪切(T)");
private JMenuItem jMenuItem23 = new JMenuItem("复制(C)");
private JMenuItem jMenuItem24 = new JMenuItem("粘贴(P)");


public Txt() {
this.setSize(500, 400);
this.setLocationRelativeTo(null);// 设置位置在中间
this.setTitle("我的记事本");// title
this.setJMenuBar(jMenuBar);
this.jScrollPane.getViewport().add(jTextArea);
this.setContentPane(this.jScrollPane);
this.jMenuBar.add(jMenu);
this.jMenuBar.add(jMenu2);
this.jMenuBar.add(jMenu3);
this.jMenuBar.add(jMenu4);
this.jMenuBar.add(jMenu5);
this.jMenu.add(jMenuItem);
this.jMenu.add(jMenuItem2);
this.jMenu.add(jMenuItem3);
this.jMenu.add(jMenuItem4);
this.jMenu2.add(jMenuItem21);
this.jMenu2.add(jMenuItem22);
this.jMenu2.add(jMenuItem23);
this.jMenu2.add(jMenuItem24);
this.jMenuItem.addActionListener(this);
this.jMenuItem2.addActionListener(this);
this.jMenuItem3.addActionListener(this);
this.jMenuItem4.addActionListener(this);
this.setVisible(true);


}


@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("新建(N)")) {
this.jTextArea.setText("");
}
if (e.getActionCommand().equals("打开(O)")) {
System.out.println("打开....");
/*
* 步骤 利用 window 文件对话框控件
*/
// 在当前窗体创建1个文件对话框控件
FileDialog fileDialog = new FileDialog(this);
fileDialog.setVisible(true);// 显示出来


// 处理 验证 用户是否点击选择了文件
String filename = fileDialog.getFile();
System.out.println("filename" + filename);


if (filename != null) {
// 选择了文件
// 文件名称 MyNotePad.java
// 文件路径
String path = fileDialog.getDirectory();
// 全路径:路径 +文件名
path = path + filename;
// System.out.println(path);
// 文件操作 <=>内容<=>文本域
File file = new File(path);
if (file.exists()) {
// 字符
try {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(
fileReader);
char[] c = new char[(int) file.length()];
bufferedReader.read(c);
// char <=>String
String s = new String(c);
// 内容<=>文本域
this.jTextArea.setText(s);


} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}


}


}


}
if (e.getActionCommand().equals("保存(S)")) {
FileDialog fileDialog = new FileDialog(this, "请选择保存的路径",
FileDialog.SAVE);
fileDialog.setVisible(true);
String filename = fileDialog.getFile();
String path2 = fileDialog.getDirectory();
File file2 = null;
if (!filename.contains(".txt")) {
file2 = new File(path2 + filename + ".txt");
} else {
file2 = new File(path2 + filename);
}
if (!file2.exists()) {
try {
file2.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}


try {
FileWriter fileWriter = new FileWriter(file2, false);
fileWriter.write(this.jTextArea.getText());
fileWriter.close();
// System.out.println();
JOptionPane.showMessageDialog(this, "写入成功");
}


catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (e.getActionCommand().equals("退出(X)")) {
System.out.println("退出...");
System.exit(0);
}
}


public static void main(String[] args) {
Txt txt = new Txt();
}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Evan9603

知识付费

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值