Java新手求指教 关于Java模拟数据库 希望高手帮帮忙

时间:2021-02-22 20:45:01
用Java语言做一个模拟数据库
 
用图形用户界面实现。
能实现输入,添加,删除,保存,查找,输出等基本功能。
输入一组数据(编号,姓名,年龄,性别)
添加到后台的表格中保存
可以用编号进行查找
可以表格的形式输出

提示:使用字符字节输入输出流或RandomAccessFile类。 
 
谁能帮忙做一个啊?很急!

15 个解决方案

#1


报价:10k

#2


模拟个啥数据库,  做这个可是要钱的!  Java新手求指教 关于Java模拟数据库 希望高手帮帮忙

#3


什么意思 第一次发帖 不懂

#4


= =
我了个去~
我们也要交这个作业 
你们的老师是不是马子睿 

#5


这个很简单啊,用一个文件就可以搞定了,文件头描述表结构(文件头固定大小,方便移动指针到数据区,每个记录的大小是固定的就可以了.就象访问数组那样理解这个文件的数据区)

#6


就是用txt文件作为数据库,数据可以用特殊字符如"-"隔开

#7


共五个文件:
DataAccessException.java
package houlei.csdn.java.swing.store;

/**
 *
 */
public class DataAccessException extends Exception {

    /**
     * Creates a new instance of <code>DataAccessException</code> without detail message.
     */
    public DataAccessException() {
    }


    /**
     * Constructs an instance of <code>DataAccessException</code> with the specified detail message.
     * @param msg the detail message.
     */
    public DataAccessException(String msg) {
        super(msg);
    }

    public DataAccessException(String message, Throwable cause) {
        super(message, cause);
    }
}

UserInfo.java
package houlei.csdn.java.swing.store;

import java.io.Serializable;

/**
 */
public class UserInfo implements Serializable{

    private String id;
    private String name;
    private int age;
    private boolean male;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public boolean isMale() {
        return male;
    }

    public void setMale(boolean male) {
        this.male = male;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

DataService.java
package houlei.csdn.java.swing.store;

/**
 *
 */
public interface DataService {

    void loadAllUserInfos() throws DataAccessException;
    void storeAllUserInfos() throws DataAccessException;
    UserInfo findUserInfo(String userId) throws DataAccessException;
    void addUserInfo(UserInfo user) throws DataAccessException;
    void deleteUserInfo(UserInfo user) throws DataAccessException;
    void updateUserInfo(UserInfo user) throws DataAccessException;
    
}

#8


FileDataService.java
利用文件进行存储的实现类
package houlei.csdn.java.swing.store;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 */
public class FileDataService implements DataService{

    private Map<String,UserInfo> cache = null;
    private File file = null;

    public FileDataService(File file) {
        this.file = file;
    }

    public void loadAllUserInfos() throws DataAccessException {
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            cache = (Map<String,UserInfo>)ois.readObject();
            ois.close();
        } catch (ClassNotFoundException ex) {
            throw new DataAccessException("存储的数据类型不正确",ex);
        } catch(java.io.EOFException ex){
            if(cache==null)cache = new TreeMap<String,UserInfo>();
        }catch (IOException ex) {
            throw new DataAccessException("文件IO出现异常",ex);
        }
        
    }

    public void storeAllUserInfos() throws DataAccessException {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(cache);
            oos.close();
        } catch (IOException ex) {
            throw new DataAccessException("文件IO出现异常",ex);
        }
    }

    public UserInfo findUserInfo(String userId) throws DataAccessException {
        return cache.get(userId);
    }

    public void addUserInfo(UserInfo user) throws DataAccessException {
        cache.put(user.getId(), user);
    }

    public void deleteUserInfo(UserInfo user) throws DataAccessException {
        cache.remove(user.getId());
    }

    public List<UserInfo> getAllUserInfo() throws DataAccessException {
        return new ArrayList<UserInfo>(cache.values());
    }

    public void updateUserInfo(UserInfo user) throws DataAccessException {
        cache.put(user.getId(), user);
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

}

#9


MainFrame.java
利用NetBeans6.8设计的一个简单界面。
package houlei.csdn.java.swing.store;
import java.io.File;
import java.io.IOException;
import javax.swing.JOptionPane;
public class MainFrame extends javax.swing.JFrame {
    public static final String filePath = "D:\\user_infos.data";
    private DataService dataService = new FileDataService(new File(filePath));
    public MainFrame() {
        initComponents();
        initDataService();
    }
    private void initDataService() {
         try {
            File file = new File(filePath);
            if(file.exists()==false){
                try {
                    file.createNewFile();
                } catch (IOException ex) {}
            }
            dataService.loadAllUserInfos();
        } catch (DataAccessException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this,ex.getCause().getMessage(),"数据文件加载失败",JOptionPane.WARNING_MESSAGE);
        }
    }
    @SuppressWarnings("unchecked")
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtfUserId = new javax.swing.JTextField();
        jtfUserName = new javax.swing.JTextField();
        jtfUserAge = new javax.swing.JTextField();
        jcbUserSex = new javax.swing.JComboBox();
        jbAdd = new javax.swing.JButton();
        jbDelete = new javax.swing.JButton();
        jbUpdate = new javax.swing.JButton();
        jbQuery = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("编号:");
        jLabel2.setText("姓名:");
        jLabel3.setText("年龄:");
        jLabel4.setText("性别:");
        jcbUserSex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "男", "女" }));
        jbAdd.setText("添加");
        jbAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAddActionPerformed(evt);
            }
        });
        jbDelete.setText("删除");
        jbDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDeleteActionPerformed(evt);
            }
        });
        jbUpdate.setText("更改");
        jbUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbUpdateActionPerformed(evt);
            }
        });
        jbQuery.setText("查询");
        jbQuery.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbQueryActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(jcbUserSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jtfUserAge, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jtfUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jtfUserId, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbAdd)
                    .addComponent(jbQuery)
                    .addComponent(jbDelete)
                    .addComponent(jbUpdate))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfUserId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbQuery))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbUpdate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jbDelete)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jtfUserAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jcbUserSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jbAdd)))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jMenu1.setText("文件");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("关闭");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);
        jMenuBar1.add(jMenu1);
        jMenu3.setText("帮助");
        jMenuBar1.add(jMenu3);
        setJMenuBar(jMenuBar1);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
    }
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }
    

#10


Java新手求指教 关于Java模拟数据库 希望高手帮帮忙顶楼上的,代码全给出了

#11


private void jbDeleteActionPerformed(java.awt.event.ActionEvent evt) {
       try {
            String queryId = jtfUserId.getText().trim();
            if (queryId.length() <= 0) {
                JOptionPane.showMessageDialog(this, "待更改的编号不能为空", "查询编号不能为空", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            UserInfo info = dataService.findUserInfo(queryId);
            if(info==null){
                JOptionPane.showMessageDialog(this, "待更改的数据并不存在", "更改的数据并不存在", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            info.setName(jtfUserName.getText());
            info.setAge(Integer.valueOf(jtfUserAge.getText().trim()));
            info.setMale(jcbUserSex.getSelectedIndex()==0);
            dataService.deleteUserInfo(info);
            dataService.storeAllUserInfos();
        } catch (DataAccessException ex) {
            JOptionPane.showMessageDialog(this,ex.getCause().getMessage(),"查询数据失败",JOptionPane.WARNING_MESSAGE);
        }
    }
    private void jbQueryActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            String queryId = jtfUserId.getText().trim();
            if (queryId.length() <= 0) {
                JOptionPane.showMessageDialog(this, "查询编号不能为空", "查询编号不能为空", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            UserInfo info = dataService.findUserInfo(queryId);
            if(info==null){
                JOptionPane.showMessageDialog(this, "查询的编号数据并不存在", "查询的编号数据并不存在", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            jtfUserName.setText(info.getName());
            jtfUserAge.setText(String.valueOf(info.getAge()));
            jcbUserSex.setSelectedIndex(info.isMale()?0:1);
        } catch (DataAccessException ex) {
            JOptionPane.showMessageDialog(this,ex.getCause().getMessage(),"查询数据失败",JOptionPane.WARNING_MESSAGE);
        }
    }
    private void jbUpdateActionPerformed(java.awt.event.ActionEvent evt) {
       try {
            String queryId = jtfUserId.getText().trim();
            if (queryId.length() <= 0) {
                JOptionPane.showMessageDialog(this, "待更改的编号不能为空", "查询编号不能为空", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            UserInfo info = dataService.findUserInfo(queryId);
            if(info==null){
                JOptionPane.showMessageDialog(this, "待更改的数据并不存在", "更改的数据并不存在", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            info.setName(jtfUserName.getText());
            info.setAge(Integer.valueOf(jtfUserAge.getText().trim()));
            info.setMale(jcbUserSex.getSelectedIndex()==0);
            dataService.updateUserInfo(info);
            dataService.storeAllUserInfos();
        } catch (DataAccessException ex) {
            JOptionPane.showMessageDialog(this,ex.getCause().getMessage(),"查询数据失败",JOptionPane.WARNING_MESSAGE);
        }
    }
    private void jbAddActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            String userId = jtfUserId.getText().trim();
            if (userId.length() <= 0) {
                JOptionPane.showMessageDialog(this, "待添加的编号不能为空", "编号不能为空", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            UserInfo info = new UserInfo();
            info.setId(jtfUserId.getText());
            info.setName(jtfUserName.getText());
            info.setAge(Integer.valueOf(jtfUserAge.getText().trim()));
            info.setMale(jcbUserSex.getSelectedIndex() == 0);
            dataService.addUserInfo(info);
            dataService.storeAllUserInfos();
        } catch (DataAccessException ex) {
            JOptionPane.showMessageDialog(this, ex.getCause().getMessage(), "添加数据失败", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbAdd;
    private javax.swing.JButton jbDelete;
    private javax.swing.JButton jbQuery;
    private javax.swing.JButton jbUpdate;
    private javax.swing.JComboBox jcbUserSex;
    private javax.swing.JTextField jtfUserAge;
    private javax.swing.JTextField jtfUserId;
    private javax.swing.JTextField jtfUserName;
}

#12


以前做过一个,关于学生信息的要实现增删改查,不过代码没了 呵呵,顶下。

#13


就是不知道那叫模拟数据库!!

#14


学习了~~

#15


让大家费心了 我已经搞定了 下面是源代码和大家分享一下
import javax.swing.*;
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
public class student extends WindowAdapter implements ActionListener,TextListener 

    Frame f; 
    TextArea ta1; 
    JTextField tf1,tf2,tf3,tf4,tf5; 
    Checkbox cb1,cb2; 

    List ls1; 
    Button b1,b2,b3,b4,b5,b6,b7,b8; 
    FileDialog fd; 
    File file1; 

    XRBFile stu=new XRBFile();//类对象

    public void display() 
   { 
      Panel p1,p2,p3; 
      CheckboxGroup cg; 
      f=new Frame("输入信息"); 
      f.setSize(500,600); 
      f.setLocation(200,140); 
      f.setBackground(Color.lightGray); 

      f.setLayout(new GridLayout(2,1)); //网格布局,上下分隔窗口 

      p1=new Panel(); 
      p1.setLayout(new GridLayout(7,1)); //网格布局,7行1列 
      f.add(p1); //占据窗口上半部分 
 
      tf1=new JTextField("(学号)"); //创建组件 
      tf2=new JTextField("(姓名)"); 
      tf3=new JTextField("(所在省份)"); 
      tf4=new JTextField("(所在市)"); 

      cg=new CheckboxGroup(); //创建复选框组 
      cb1=new Checkbox("男",cg,true); //创建单选按钮 
      cb2=new Checkbox("女",cg,false);
      tf5=new JTextField("存储文件名"); 

      b1=new Button("第一条记录"); 
      b1.addActionListener(this); 
      b2=new Button("上一条记录"); 
      b2.addActionListener(this); 
      b3=new Button("下一条记录"); 
      b3.addActionListener(this); 
      b4=new Button("最后一条记录"); 
      b4.addActionListener(this); 
      b5=new Button("添加"); 
      b5.addActionListener(this); 
      b6=new Button("删除"); 
      b6.addActionListener(this); 

      b7=new Button("打开"); 
      b7.addActionListener(this); 
      b8=new Button("保存"); 
      b8.addActionListener(this); 
      p1.add(tf1); //组件依次添加到面板pl上 
      p1.add(tf2); 
      p2=new Panel(); 
      p2.setLayout(new FlowLayout(FlowLayout.LEFT)); 
      p2.add(cb1); 
      p2.add(cb2); 
      p1.add(p2); 
      p1.add(tf3); 
      p1.add(tf4); 
      p1.add(tf5); 
      p3=new Panel(); 
      p3.setLayout(new GridLayout(2,4)); 
      p3.add(b1); 
      p3.add(b2); 
      p3.add(b3); 
      p3.add(b4); 
      p3.add(b5); 
      p3.add(b6); 
      p3.add(b7); 
      p3.add(b8); 
      p1.add(p3); 
      ls1=new List(); //创建列表框 
      f.add(ls1); //占据窗口下半部分 
      f.addWindowListener(this); 
      f.setVisible(true); 
     } 
     public void textValueChanged(TextEvent e) 
     { //实现TextListener接口中的方法,对文本区操作时触发 
         b8.setEnabled(true); 
      } 

     public void windowClosing(WindowEvent e) 
    { 
       System.exit(0); 
     } 


     public void actionPerformed(ActionEvent e) 
     { 
          if(e.getSource()==b1) //单第一条记录按钮时 
          { 
              int i; 
              ls1.select(0); 
           } 


          if(e.getSource()==b2) //单击上一条记录按钮时 
          { 
              int i; 
              i=ls1.getSelectedIndex(); 
              ls1.select(i-1); 
           } 


           if(e.getSource()==b3) //单击下一条记录按钮时 
           { 
               int i; 
               i=ls1.getSelectedIndex(); 
               ls1.select(i+1); 
              } 


            if(e.getSource()==b4) //单击最后一条记录按钮时 
            { 
                int i; 
                 i=ls1.getItemCount(); 
                ls1.select(i-1); 
               } 


            if(e.getSource()==b5) //单击添加按钮时 
            { 

               String str;

               str=(tf1.getText()+"   "+tf2.getText()+"    "+tf3.getText()+"   "+tf4.getText());

               ls1.add(str); //添加列表框选项 

               } //编号自动加1 


              if(e.getSource()==b6)//单击删除按钮时 
             { 
                 int i; 
                 i=ls1.getSelectedIndex(); 
                 ls1.remove(i); 

             } //编号自动减1 


              if(e.getSource()==b7) //单击打开按钮时 
               { 
    
  
                   fd=new FileDialog(f,"打开",FileDialog.LOAD); 
                    fd.setVisible(true); //创建并显示打开文件对话框 
                   if((fd.getDirectory()!=null)&&(fd.getFile()!=null)) 
                   { 
                      tf5.setText(fd.getDirectory()+fd.getFile()); 
                      try //以缓冲区方式读取文件内容 
                      { 
                          file1=new File(fd.getDirectory(),fd.getFile()); 
                          FileReader fr=new FileReader(file1); 
                          BufferedReader br=new BufferedReader(fr); 
                           String aline; 
                          while((aline=br.readLine())!=null) //按行读取文本 
                              ta1.append(aline+"\r\n"); 
                          fr.close(); 
                          br.close(); 
                        }
     
                       catch(IOException ioe) 
                      { 
                          System.out.println(ioe); 
                       } 
                     } 
 

                   }
              if(e.getSource()==b8) //单击保存按钮时 
              { 
                  if((e.getSource()==b8)&&(file1==null)) 
            //单击保存按钮且文件对象为空时 
                   { 
                      fd=new FileDialog(f,"保存",FileDialog.SAVE); 
                      if(file1==null) 
                       fd.setFile("xrb.txt"); 
                      else 
                        fd.setFile(file1.getName());
                        //fd.setFile(aline.getName());
                        fd.setVisible(true); //创建并显示保存文件对话框 

                     if((fd.getDirectory()!=null)&&(fd.getFile()!=null)) 
                     { 
                            tf5.setText(fd.getDirectory()+fd.getFile()); 
                            file1=new File(fd.getDirectory(),fd.getFile()); 
                            
                            
                            String str;
                            str=(tf1.getText()+"   "+tf2.getText()+"    "+tf3.getText()+"   "+tf4.getText());
                            stu.writeToFile(str);
                            
                            
                      } 
     
                    
                       } 
                   }
                } 

   
    
    class XRBFile//文件类
    {
    
      public void writeToFile(String aline)
      { 
          try //将文本区内容写入字符输出流 
         { 
             FileWriter fw=new FileWriter(file1); 
            
             fw.write(aline);
             fw.close(); 
           
          } 
          catch(IOException ioe) 
         { 
              System.out.println(ioe); 
          } 
       finally{
        }


       }
       
      }
    

public static void main(String arg[]) 

   new student().display(); 

}

#1


报价:10k

#2


模拟个啥数据库,  做这个可是要钱的!  Java新手求指教 关于Java模拟数据库 希望高手帮帮忙

#3


什么意思 第一次发帖 不懂

#4


= =
我了个去~
我们也要交这个作业 
你们的老师是不是马子睿 

#5


这个很简单啊,用一个文件就可以搞定了,文件头描述表结构(文件头固定大小,方便移动指针到数据区,每个记录的大小是固定的就可以了.就象访问数组那样理解这个文件的数据区)

#6


就是用txt文件作为数据库,数据可以用特殊字符如"-"隔开

#7


共五个文件:
DataAccessException.java
package houlei.csdn.java.swing.store;

/**
 *
 */
public class DataAccessException extends Exception {

    /**
     * Creates a new instance of <code>DataAccessException</code> without detail message.
     */
    public DataAccessException() {
    }


    /**
     * Constructs an instance of <code>DataAccessException</code> with the specified detail message.
     * @param msg the detail message.
     */
    public DataAccessException(String msg) {
        super(msg);
    }

    public DataAccessException(String message, Throwable cause) {
        super(message, cause);
    }
}

UserInfo.java
package houlei.csdn.java.swing.store;

import java.io.Serializable;

/**
 */
public class UserInfo implements Serializable{

    private String id;
    private String name;
    private int age;
    private boolean male;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public boolean isMale() {
        return male;
    }

    public void setMale(boolean male) {
        this.male = male;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

DataService.java
package houlei.csdn.java.swing.store;

/**
 *
 */
public interface DataService {

    void loadAllUserInfos() throws DataAccessException;
    void storeAllUserInfos() throws DataAccessException;
    UserInfo findUserInfo(String userId) throws DataAccessException;
    void addUserInfo(UserInfo user) throws DataAccessException;
    void deleteUserInfo(UserInfo user) throws DataAccessException;
    void updateUserInfo(UserInfo user) throws DataAccessException;
    
}

#8


FileDataService.java
利用文件进行存储的实现类
package houlei.csdn.java.swing.store;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 */
public class FileDataService implements DataService{

    private Map<String,UserInfo> cache = null;
    private File file = null;

    public FileDataService(File file) {
        this.file = file;
    }

    public void loadAllUserInfos() throws DataAccessException {
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            cache = (Map<String,UserInfo>)ois.readObject();
            ois.close();
        } catch (ClassNotFoundException ex) {
            throw new DataAccessException("存储的数据类型不正确",ex);
        } catch(java.io.EOFException ex){
            if(cache==null)cache = new TreeMap<String,UserInfo>();
        }catch (IOException ex) {
            throw new DataAccessException("文件IO出现异常",ex);
        }
        
    }

    public void storeAllUserInfos() throws DataAccessException {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(cache);
            oos.close();
        } catch (IOException ex) {
            throw new DataAccessException("文件IO出现异常",ex);
        }
    }

    public UserInfo findUserInfo(String userId) throws DataAccessException {
        return cache.get(userId);
    }

    public void addUserInfo(UserInfo user) throws DataAccessException {
        cache.put(user.getId(), user);
    }

    public void deleteUserInfo(UserInfo user) throws DataAccessException {
        cache.remove(user.getId());
    }

    public List<UserInfo> getAllUserInfo() throws DataAccessException {
        return new ArrayList<UserInfo>(cache.values());
    }

    public void updateUserInfo(UserInfo user) throws DataAccessException {
        cache.put(user.getId(), user);
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

}

#9


MainFrame.java
利用NetBeans6.8设计的一个简单界面。
package houlei.csdn.java.swing.store;
import java.io.File;
import java.io.IOException;
import javax.swing.JOptionPane;
public class MainFrame extends javax.swing.JFrame {
    public static final String filePath = "D:\\user_infos.data";
    private DataService dataService = new FileDataService(new File(filePath));
    public MainFrame() {
        initComponents();
        initDataService();
    }
    private void initDataService() {
         try {
            File file = new File(filePath);
            if(file.exists()==false){
                try {
                    file.createNewFile();
                } catch (IOException ex) {}
            }
            dataService.loadAllUserInfos();
        } catch (DataAccessException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this,ex.getCause().getMessage(),"数据文件加载失败",JOptionPane.WARNING_MESSAGE);
        }
    }
    @SuppressWarnings("unchecked")
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtfUserId = new javax.swing.JTextField();
        jtfUserName = new javax.swing.JTextField();
        jtfUserAge = new javax.swing.JTextField();
        jcbUserSex = new javax.swing.JComboBox();
        jbAdd = new javax.swing.JButton();
        jbDelete = new javax.swing.JButton();
        jbUpdate = new javax.swing.JButton();
        jbQuery = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("编号:");
        jLabel2.setText("姓名:");
        jLabel3.setText("年龄:");
        jLabel4.setText("性别:");
        jcbUserSex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "男", "女" }));
        jbAdd.setText("添加");
        jbAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAddActionPerformed(evt);
            }
        });
        jbDelete.setText("删除");
        jbDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDeleteActionPerformed(evt);
            }
        });
        jbUpdate.setText("更改");
        jbUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbUpdateActionPerformed(evt);
            }
        });
        jbQuery.setText("查询");
        jbQuery.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbQueryActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(jcbUserSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jtfUserAge, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jtfUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jtfUserId, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbAdd)
                    .addComponent(jbQuery)
                    .addComponent(jbDelete)
                    .addComponent(jbUpdate))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfUserId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbQuery))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbUpdate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jbDelete)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jtfUserAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jcbUserSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jbAdd)))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jMenu1.setText("文件");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("关闭");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);
        jMenuBar1.add(jMenu1);
        jMenu3.setText("帮助");
        jMenuBar1.add(jMenu3);
        setJMenuBar(jMenuBar1);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
    }
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }
    

#10


Java新手求指教 关于Java模拟数据库 希望高手帮帮忙顶楼上的,代码全给出了

#11


private void jbDeleteActionPerformed(java.awt.event.ActionEvent evt) {
       try {
            String queryId = jtfUserId.getText().trim();
            if (queryId.length() <= 0) {
                JOptionPane.showMessageDialog(this, "待更改的编号不能为空", "查询编号不能为空", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            UserInfo info = dataService.findUserInfo(queryId);
            if(info==null){
                JOptionPane.showMessageDialog(this, "待更改的数据并不存在", "更改的数据并不存在", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            info.setName(jtfUserName.getText());
            info.setAge(Integer.valueOf(jtfUserAge.getText().trim()));
            info.setMale(jcbUserSex.getSelectedIndex()==0);
            dataService.deleteUserInfo(info);
            dataService.storeAllUserInfos();
        } catch (DataAccessException ex) {
            JOptionPane.showMessageDialog(this,ex.getCause().getMessage(),"查询数据失败",JOptionPane.WARNING_MESSAGE);
        }
    }
    private void jbQueryActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            String queryId = jtfUserId.getText().trim();
            if (queryId.length() <= 0) {
                JOptionPane.showMessageDialog(this, "查询编号不能为空", "查询编号不能为空", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            UserInfo info = dataService.findUserInfo(queryId);
            if(info==null){
                JOptionPane.showMessageDialog(this, "查询的编号数据并不存在", "查询的编号数据并不存在", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            jtfUserName.setText(info.getName());
            jtfUserAge.setText(String.valueOf(info.getAge()));
            jcbUserSex.setSelectedIndex(info.isMale()?0:1);
        } catch (DataAccessException ex) {
            JOptionPane.showMessageDialog(this,ex.getCause().getMessage(),"查询数据失败",JOptionPane.WARNING_MESSAGE);
        }
    }
    private void jbUpdateActionPerformed(java.awt.event.ActionEvent evt) {
       try {
            String queryId = jtfUserId.getText().trim();
            if (queryId.length() <= 0) {
                JOptionPane.showMessageDialog(this, "待更改的编号不能为空", "查询编号不能为空", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            UserInfo info = dataService.findUserInfo(queryId);
            if(info==null){
                JOptionPane.showMessageDialog(this, "待更改的数据并不存在", "更改的数据并不存在", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            info.setName(jtfUserName.getText());
            info.setAge(Integer.valueOf(jtfUserAge.getText().trim()));
            info.setMale(jcbUserSex.getSelectedIndex()==0);
            dataService.updateUserInfo(info);
            dataService.storeAllUserInfos();
        } catch (DataAccessException ex) {
            JOptionPane.showMessageDialog(this,ex.getCause().getMessage(),"查询数据失败",JOptionPane.WARNING_MESSAGE);
        }
    }
    private void jbAddActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            String userId = jtfUserId.getText().trim();
            if (userId.length() <= 0) {
                JOptionPane.showMessageDialog(this, "待添加的编号不能为空", "编号不能为空", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            UserInfo info = new UserInfo();
            info.setId(jtfUserId.getText());
            info.setName(jtfUserName.getText());
            info.setAge(Integer.valueOf(jtfUserAge.getText().trim()));
            info.setMale(jcbUserSex.getSelectedIndex() == 0);
            dataService.addUserInfo(info);
            dataService.storeAllUserInfos();
        } catch (DataAccessException ex) {
            JOptionPane.showMessageDialog(this, ex.getCause().getMessage(), "添加数据失败", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbAdd;
    private javax.swing.JButton jbDelete;
    private javax.swing.JButton jbQuery;
    private javax.swing.JButton jbUpdate;
    private javax.swing.JComboBox jcbUserSex;
    private javax.swing.JTextField jtfUserAge;
    private javax.swing.JTextField jtfUserId;
    private javax.swing.JTextField jtfUserName;
}

#12


以前做过一个,关于学生信息的要实现增删改查,不过代码没了 呵呵,顶下。

#13


就是不知道那叫模拟数据库!!

#14


学习了~~

#15


让大家费心了 我已经搞定了 下面是源代码和大家分享一下
import javax.swing.*;
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
public class student extends WindowAdapter implements ActionListener,TextListener 

    Frame f; 
    TextArea ta1; 
    JTextField tf1,tf2,tf3,tf4,tf5; 
    Checkbox cb1,cb2; 

    List ls1; 
    Button b1,b2,b3,b4,b5,b6,b7,b8; 
    FileDialog fd; 
    File file1; 

    XRBFile stu=new XRBFile();//类对象

    public void display() 
   { 
      Panel p1,p2,p3; 
      CheckboxGroup cg; 
      f=new Frame("输入信息"); 
      f.setSize(500,600); 
      f.setLocation(200,140); 
      f.setBackground(Color.lightGray); 

      f.setLayout(new GridLayout(2,1)); //网格布局,上下分隔窗口 

      p1=new Panel(); 
      p1.setLayout(new GridLayout(7,1)); //网格布局,7行1列 
      f.add(p1); //占据窗口上半部分 
 
      tf1=new JTextField("(学号)"); //创建组件 
      tf2=new JTextField("(姓名)"); 
      tf3=new JTextField("(所在省份)"); 
      tf4=new JTextField("(所在市)"); 

      cg=new CheckboxGroup(); //创建复选框组 
      cb1=new Checkbox("男",cg,true); //创建单选按钮 
      cb2=new Checkbox("女",cg,false);
      tf5=new JTextField("存储文件名"); 

      b1=new Button("第一条记录"); 
      b1.addActionListener(this); 
      b2=new Button("上一条记录"); 
      b2.addActionListener(this); 
      b3=new Button("下一条记录"); 
      b3.addActionListener(this); 
      b4=new Button("最后一条记录"); 
      b4.addActionListener(this); 
      b5=new Button("添加"); 
      b5.addActionListener(this); 
      b6=new Button("删除"); 
      b6.addActionListener(this); 

      b7=new Button("打开"); 
      b7.addActionListener(this); 
      b8=new Button("保存"); 
      b8.addActionListener(this); 
      p1.add(tf1); //组件依次添加到面板pl上 
      p1.add(tf2); 
      p2=new Panel(); 
      p2.setLayout(new FlowLayout(FlowLayout.LEFT)); 
      p2.add(cb1); 
      p2.add(cb2); 
      p1.add(p2); 
      p1.add(tf3); 
      p1.add(tf4); 
      p1.add(tf5); 
      p3=new Panel(); 
      p3.setLayout(new GridLayout(2,4)); 
      p3.add(b1); 
      p3.add(b2); 
      p3.add(b3); 
      p3.add(b4); 
      p3.add(b5); 
      p3.add(b6); 
      p3.add(b7); 
      p3.add(b8); 
      p1.add(p3); 
      ls1=new List(); //创建列表框 
      f.add(ls1); //占据窗口下半部分 
      f.addWindowListener(this); 
      f.setVisible(true); 
     } 
     public void textValueChanged(TextEvent e) 
     { //实现TextListener接口中的方法,对文本区操作时触发 
         b8.setEnabled(true); 
      } 

     public void windowClosing(WindowEvent e) 
    { 
       System.exit(0); 
     } 


     public void actionPerformed(ActionEvent e) 
     { 
          if(e.getSource()==b1) //单第一条记录按钮时 
          { 
              int i; 
              ls1.select(0); 
           } 


          if(e.getSource()==b2) //单击上一条记录按钮时 
          { 
              int i; 
              i=ls1.getSelectedIndex(); 
              ls1.select(i-1); 
           } 


           if(e.getSource()==b3) //单击下一条记录按钮时 
           { 
               int i; 
               i=ls1.getSelectedIndex(); 
               ls1.select(i+1); 
              } 


            if(e.getSource()==b4) //单击最后一条记录按钮时 
            { 
                int i; 
                 i=ls1.getItemCount(); 
                ls1.select(i-1); 
               } 


            if(e.getSource()==b5) //单击添加按钮时 
            { 

               String str;

               str=(tf1.getText()+"   "+tf2.getText()+"    "+tf3.getText()+"   "+tf4.getText());

               ls1.add(str); //添加列表框选项 

               } //编号自动加1 


              if(e.getSource()==b6)//单击删除按钮时 
             { 
                 int i; 
                 i=ls1.getSelectedIndex(); 
                 ls1.remove(i); 

             } //编号自动减1 


              if(e.getSource()==b7) //单击打开按钮时 
               { 
    
  
                   fd=new FileDialog(f,"打开",FileDialog.LOAD); 
                    fd.setVisible(true); //创建并显示打开文件对话框 
                   if((fd.getDirectory()!=null)&&(fd.getFile()!=null)) 
                   { 
                      tf5.setText(fd.getDirectory()+fd.getFile()); 
                      try //以缓冲区方式读取文件内容 
                      { 
                          file1=new File(fd.getDirectory(),fd.getFile()); 
                          FileReader fr=new FileReader(file1); 
                          BufferedReader br=new BufferedReader(fr); 
                           String aline; 
                          while((aline=br.readLine())!=null) //按行读取文本 
                              ta1.append(aline+"\r\n"); 
                          fr.close(); 
                          br.close(); 
                        }
     
                       catch(IOException ioe) 
                      { 
                          System.out.println(ioe); 
                       } 
                     } 
 

                   }
              if(e.getSource()==b8) //单击保存按钮时 
              { 
                  if((e.getSource()==b8)&&(file1==null)) 
            //单击保存按钮且文件对象为空时 
                   { 
                      fd=new FileDialog(f,"保存",FileDialog.SAVE); 
                      if(file1==null) 
                       fd.setFile("xrb.txt"); 
                      else 
                        fd.setFile(file1.getName());
                        //fd.setFile(aline.getName());
                        fd.setVisible(true); //创建并显示保存文件对话框 

                     if((fd.getDirectory()!=null)&&(fd.getFile()!=null)) 
                     { 
                            tf5.setText(fd.getDirectory()+fd.getFile()); 
                            file1=new File(fd.getDirectory(),fd.getFile()); 
                            
                            
                            String str;
                            str=(tf1.getText()+"   "+tf2.getText()+"    "+tf3.getText()+"   "+tf4.getText());
                            stu.writeToFile(str);
                            
                            
                      } 
     
                    
                       } 
                   }
                } 

   
    
    class XRBFile//文件类
    {
    
      public void writeToFile(String aline)
      { 
          try //将文本区内容写入字符输出流 
         { 
             FileWriter fw=new FileWriter(file1); 
            
             fw.write(aline);
             fw.close(); 
           
          } 
          catch(IOException ioe) 
         { 
              System.out.println(ioe); 
          } 
       finally{
        }


       }
       
      }
    

public static void main(String arg[]) 

   new student().display(); 

}