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

📄 textfileviewer.java

📁 文件查看器 创建一个包含TextArea区域的窗口,在该区域中显示打开的文本文件的内容
💻 JAVA
字号:
/**
* 本案例创建一个包含TextArea区域的窗口,
* 在该区域中显示打开的文本文件的内容.
**/

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class TextFileViewer extends Frame implements ActionListener {
    String directory; // 在FileDialog中显示的默认目录

    TextArea textarea; // 显示文本文件的区域

    // 构造方法: 打开一个文件浏览器
    public TextFileViewer() {
        this(null, null);
    }

    // 构造方法: 显示当前目录下的文件
    public TextFileViewer(String filename) {
        this(null, filename);
    }

    // 构造方法: 创建用来显示指定目录中的指定文件内容的TextFileViewer对象
    public TextFileViewer(String directory, String filename) {
        super(); // 创建框架
        // 当用户请求时销毁窗口
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        });
        // 创建一个用来显示文件内容的TextArea区域
        textarea = new TextArea("", 24, 80);
        textarea.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
        textarea.setEditable(false);
        this.add("Center", textarea);
        // 创建一个包含两个按钮控件的面板
        Panel myPanel = new Panel();
        myPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));
        this.add(myPanel, "South");
        // 创建按钮控件,并且处理单击按钮事件
        Font font = new Font("SansSerif", Font.BOLD, 14);
        Button btnOpenFile = new Button("打开文件");
        Button btnClose = new Button("关闭");
        btnOpenFile.addActionListener(this);
        btnOpenFile.setActionCommand("open");
        btnOpenFile.setFont(font);
        btnClose.addActionListener(this);
        btnClose.setActionCommand("close");
        btnClose.setFont(font);
        myPanel.add(btnOpenFile);
        myPanel.add(btnClose);
        this.pack();
        // 指明目录
        if (directory == null) {
            File f;
            if ((filename != null) && (f = new File(filename)).isAbsolute()) {
                directory = f.getParent();
                filename = f.getName();
            } else
                directory = System.getProperty("user.dir");
        }
        this.directory = directory; // 记住目录
        setFile(directory, filename); // 载入并显示文件
    }

    //载入并且显示指定目录中的指定文件内容
    public void setFile(String directory, String filename) {
        if ((filename == null) || (filename.length() == 0))
            return;
        File f;
        FileReader in = null;
        // 读取并且显示文件内容
        // 因为是在读取文本,所以使用FileReader,而不使用FileInputStream.
        try {
            f = new File(directory, filename); // 创建一个file对象
            in = new FileReader(f);               // 和一个用来读取它的字符流
            char[] buffer = new char[4096]; // 每次读取4K字符
            int len;                                       // 每次读入的字符数
            textarea.setText("");                   // 清除文本区域
            while ((len = in.read(buffer)) != -1) { // 读取一批字符
                String s = new String(buffer, 0, len); // 转化为一个字符串
                textarea.append(s); // 显示文本
            }
            this.setTitle("TextFileViewer: " + filename); // 设置窗口标题
            textarea.setCaretPosition(0); // 设置文件起始位置
        }
        // 显示错误信息
        catch (IOException e) {
            textarea.setText(e.getClass().getName() + ": " + e.getMessage());
            this.setTitle("TextFileViewer: " + filename + ": I/O Exception");
        }
        // 关闭输入流
        finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
    }

    //处理单击按钮事件
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if (cmd.equals("open")) { // 如果用户单击了“打开”按钮
            // 创建一个文件对话框,提示输入新的文件
            FileDialog myFileDialog = new FileDialog(this, "Open File", FileDialog.LOAD);
            myFileDialog.setDirectory(directory); // 设置默认目录
            // 显示对话框并等待用户的输入
            myFileDialog.show();
            directory = myFileDialog.getDirectory(); // 记住新的默认目录
            setFile(directory, myFileDialog.getFile()); // 载入并显示选择
            myFileDialog.dispose(); // 关闭对话框
        } else if (cmd.equals("close")) // 如果用户单击了“关闭”按钮
            this.dispose(); //关闭窗口
    }

    //TextFileViewer可以被其他程序所使用
    //也可以加上main()方法,成为一个独立程序.
    static public void main(String[] args) throws IOException {
        // 创建一个TextFileViewer对象
        Frame myFrame = new TextFileViewer((args.length == 1) ? args[0] : null);
        // 当TextFileViewer窗口关闭时,退出
        myFrame.addWindowListener(new WindowAdapter() {
            public void windowClosed(WindowEvent e) {
                System.exit(0);
            }
        });
        // 弹出一个窗口
        myFrame.show();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -