⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rafexam.java

📁 Java程序设计实用教程源代码 本书源代码按章分别放置在不同的文件夹中,所有程序均在JDK1.6环境下编译运行正常,除了第13章需要建立ODBC数据源之外,其他程序只要有Java运行环境即可直接运行
💻 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 + -