zoukankan      html  css  js  c++  java
  • 英汉小词典 java随机存取文件流应用 version1.0

    package jnet;
    //英汉小词典  随机存取文件   需改进 version1.0
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class F extends JFrame implements ActionListener{
        File file = new File("英汉小词典.txt");
        JButton writeBtn = new JButton("录入");
        JButton viewBtn = new JButton("显示");
        JTextField word = new JTextField(8);
        JTextField note = new JTextField(8);
        JTextArea txt = new JTextArea(20,30);
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        
        F(){
            setTitle("英汉小词典");
            setBounds(100,50,400,250);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            
            add(p1,"North");
            p1.setBackground(Color.cyan);
            p1.add(new JLabel("输入单词"));
            p1.add(word);
            p1.add(new JLabel("输入解释"));
            p1.add(note);
            p1.add(writeBtn);
            writeBtn.addActionListener(this);
            
            add(p2,"Center");
            p2.add(viewBtn);
            p2.add(txt);
            viewBtn.addActionListener(this);
            validate();
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource()==writeBtn) {
                inputWord();
            }
            if (e.getSource()==viewBtn) {
                viewWord();
            }
        }
        
        //显示汉语解释的方法
        public void viewWord()
        {
            int number=1;
            try {
                @SuppressWarnings("resource")
                RandomAccessFile infile = new RandomAccessFile(file, "rw");
                @SuppressWarnings("unused")
                String 单词 =null;
                while((单词=infile.readUTF())!=null)
                {
                    txt.append("\n"+number+单词);
                    txt.append(" " + infile.readUTF());  //读取汉语解释
                    txt.append("\n--------------------");
                    number++;
                }
                infile.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //处理输入的单词的
        public void inputWord()
        {  
            try {
                RandomAccessFile outfile = new RandomAccessFile(file, "rw");
                if (file.exists()) {
                    //实现指针跳到文件末尾
                    long length = file.length();
                    outfile.seek(length);
                }
                outfile.writeUTF("单词:"+word.getText());
                outfile.writeUTF("解释:"+note.getText());
                outfile.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            new F();
        }
    }

  • 相关阅读:
    让vs2013自带的IISExpress支持apk文件下载
    使用h5开发跨平台APP确保数据安全交互---服务器篇
    如何使用iis发布多个ftp,为何ftp 503错误?
    HttpApplication执行顺序
    EXCEL2010分成多个窗口的,解决单个窗口显示多个文档的弊病
    搜索引擎优化(SEO)解决方案
    .net网站快速停机设置app_offline
    SQL SERVER 连接 SQL SERVER 连接服务器
    SQL SERVER 备份还原 局域网 远程
    postgres 临时表
  • 原文地址:https://www.cnblogs.com/nanfengnan/p/13849636.html
Copyright © 2011-2022 走看看