📄 rafexam.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class rafExam {
public static void main(String args[]) {
new FileFrame();
}
}
class FileFrame extends Frame implements ActionListener {
TextArea ta;
Button open, save, quit;
FileDialog fd; //文件对话框
FileFrame() {
super("读写文件");
ta = new TextArea(10, 45);
open = new Button("打开");
save = new Button("另存为");
quit = new Button("关闭");
open.addActionListener(this);
save.addActionListener(this);
quit.addActionListener(this);
setLayout(new FlowLayout());
add(ta);
add(open);
add(save);
add(quit);
setSize(350, 250);
setVisible(true);
setResizable(false);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "打开") {
fd = new FileDialog(this, "打开文件", FileDialog.LOAD);
//设置文件对话框的初始目录为当前目录
fd.setDirectory(System.getProperty("user.dir"));
fd.setVisible(true); //打开文件对话框
try {
ta.setText("");
File myfile = new File(fd.getDirectory(), fd.getFile());
RandomAccessFile raf = new RandomAccessFile(myfile, "r");
while (raf.getFilePointer() < raf.length()) {
ta.append(raf.readLine() + "\n");
}
raf.close();
}
catch (IOException ioe) {
System.err.println(ioe.toString());
}
}
if (e.getActionCommand() == "另存为") {
try {
fd = new FileDialog(this, "保存文件", FileDialog.SAVE);
fd.setDirectory(System.getProperty("user.dir")); //设置初始目录
fd.setVisible(true); //打开文件对话框
File myfile = new File(fd.getDirectory(), fd.getFile());
RandomAccessFile sf = new RandomAccessFile(myfile, "rw");
sf.setLength(0);
sf.writeBytes(ta.getText().toString());
sf.close();
}
catch (IOException ee) {
System.out.println(ee.toString());
}
}
if (e.getActionCommand() == "关闭") {
dispose();
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -