读取顺序存储的文件


import  java.awt.BorderLayout;
import  java.awt.GridLayout;

import  javax.swing.JButton;
import  javax.swing.JLabel;
import  javax.swing.JPanel;
import  javax.swing.JTextField;

public   class  BankUI  extends  JPanel {
    
    
protected final static String names[] = {"Account number","First name","Last name","Balance","Transaction Amount"    };
    
    
protected JLabel labels[];
    
protected JTextField fields[];
    
protected JButton doTask1,doTask2;
    
protected JPanel innerPanelCenter,innerPanelSouth;
    
    
protected int size;
    
    
public static final int ACCOUNT = 0,FIRSTNAME = 1, LASTNAME =2,BALANCE=3,TRANSACTION=4;
    
    
public BankUI(int mySize)
    
{
        size 
= mySize;
        labels 
= new JLabel[size];
        fields 
= new JTextField[size];
        
        
for(int i=0;i<labels.length;i++)
            labels[i] 
= new JLabel(names[i]);
        
        
for(int i=0;i<fields.length;i++)
            fields[i] 
= new JTextField();
        
        innerPanelCenter 
= new JPanel();
        innerPanelCenter.setLayout(
new GridLayout(size,2));
        
        
for(int i=0;i<size;i++){
            innerPanelCenter.add(labels[i]);
            innerPanelCenter.add(fields[i]);
        }

        
        doTask1 
=new JButton();
        doTask2 
= new JButton();
        
        innerPanelSouth 
= new JPanel();
        innerPanelSouth.add(doTask1);
        innerPanelSouth.add(doTask2);
        
        setLayout(
new BorderLayout());
        add(innerPanelCenter,BorderLayout.CENTER);
        add(innerPanelSouth,BorderLayout.SOUTH);
        
        validate();
    }

    
    
public JButton getDoTask1Button(){
        
return doTask1;
    }

    
    
public JButton getDoTask2Button(){
        
return doTask2;
    }

    
public JTextField[] getFields(){
        
return fields;
    }

    
public void clearFields(){
        
for(int i=0;i<size;i++)
            fields[i].setText(
"");
    }

    
public void setFieldValues(String strings[])throws IllegalArgumentException{
        
if(strings.length != size)
            
throw new IllegalArgumentException("There must be" +
                    size 
+ "Strings in the array");
        
for(int i=0;i<size;i++)
            fields[i].setText(strings[i]);
    }

    
public String[] getFieldValues(){
        String values[] 
= new String[size];
        
for(int i=0;i<size;i++)
            values[i] 
= fields[i].getText();
        
return values;
    }

    
    
}



// AccountRecord方法主要是维护一个帐号的信息。
import  java.io.Serializable;
// Serializable是一个标记接口。(标记接口不含任何方法)实现该接口能使ObjectOutputStream和
// ObjectInputStream使用AccountRecord对象。
public   class  AccountRecord  implements  Serializable {

    
private int account;
    
private String firstName;
    
private String lastName;
    
private double balance;
    
    
public AccountRecord()
    
{
        
this(0,"","",0.0);
    }


    
public AccountRecord(int acct, String first, String last, double bal) {
        
// TODO 自动生成构造函数存根
        setAccount(acct);
        setFirstName(first);
        setFirstName(last);
        setBalance(bal);
    }


    
public int getAccount() {
        
return account;
    }


    
public void setAccount(int account) {
        
this.account = account;
    }


    
public String getFirstName() {
        
return firstName;
    }


    
public void setFirstName(String firstName) {
        
this.firstName = firstName;
    }


    
public String getLastName() {
        
return lastName;
    }


    
public void setLastName(String lastName) {
        
this.lastName = lastName;
    }


    
public double getBalance() {
        
return balance;
    }


    
public void setBalance(double balance) {
        
this.balance = balance;
    }


    
    
}

 

 

package  com.hz.file;

// 读取顺序存储的文件
import  java.awt.BorderLayout;
import  java.awt.event.ActionEvent;
import  java.awt.event.ActionListener;
import  java.awt.event.WindowAdapter;
import  java.awt.event.WindowEvent;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.IOException;
import  java.io.ObjectInputStream;

import  javax.swing.JButton;
import  javax.swing.JFileChooser;
import  javax.swing.JFrame;
import  javax.swing.JOptionPane;

import  com.hz.FileandIO.AccountRecord;
import  com.hz.FileandIO.BankUI;

public   class  ReadSequentialFile  extends  JFrame  {

    
private ObjectInputStream input;
    
private BankUI userInterface;
    
private JButton nextButton, openButton;
    
    
public ReadSequentialFile()
    
{
        
super("顺序读取文件");
        
        userInterface 
= new BankUI(4);
        getContentPane().add(userInterface,BorderLayout.CENTER);
        
        openButton 
= userInterface.getDoTask1Button();
        openButton.setText(
"打开文件");
        
        openButton.addActionListener(
new ActionListener()
        
{
            
public void actionPerformed(ActionEvent event)
            
{
                openFile();
            }

        }

        );
        
        addWindowListener(
            
new WindowAdapter(){
                
public void windowClosing(WindowEvent event)
                
{
                    
if(input != null)
                        closeFile();
                    System.exit(
0);
                }

            }

        );
        
        
        nextButton 
= userInterface.getDoTask2Button();
        nextButton.setText(
"下一个 Record");
        nextButton.setEnabled(
false);
        
        nextButton.addActionListener(
                
new ActionListener()
                
{
                    
public void actionPerformed(ActionEvent event)
                    
{
                        readRecord();
                    }

                }

        );
        
        pack();
        setSize(
300,200);
        setVisible(
true);
        
    }

    
    
private void openFile()
    
{
        JFileChooser fileChooser 
= new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        
        
int result = fileChooser.showOpenDialog(this);
        
        
if(result == JFileChooser.CANCEL_OPTION)
            
return;
        
        File fileName 
= fileChooser.getSelectedFile();
        
        
if(fileName == null || fileName.getName().equals(""))
            JOptionPane.showMessageDialog(
this,"Invalid file Name",
                    
"Invalid File Name",JOptionPane.ERROR_MESSAGE);
        
        
else
            
try{
                FileInputStream fis 
= new FileInputStream(fileName);
                input 
= new ObjectInputStream(fis);    
                openButton.setEnabled(
false);
                openButton.setEnabled(
true);
            }
catch(IOException e){
                JOptionPane.showMessageDialog(
this"Error opening file""Error", JOptionPane.ERROR_MESSAGE);
            }

    }

    
    
private void closeFile()
    
{
        
try {
            input.close();
            System.exit(
0);
        }
 catch (IOException e) {
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
    }

    
    
private void readRecord()
    
{
        AccountRecord record;
        
        
try {
            record 
= (AccountRecord)input.readObject();
            String values[] 
= { String.valueOf(record.getAccount()),record.getFirstName(),
                    record.getLastName(),String.valueOf(record.getBalance()) }
;
            
            userInterface.setFieldValues(values);
        }
 catch (IOException e) {
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }
 catch (ClassNotFoundException e) {
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
    }

    
    
public static void main(String args[]){
        
new ReadSequentialFile();
    }

}

 参照创建顺序存取文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值