📄 readfile.java
字号:
import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ReadFile extends JFrame { private ObjectInputStream input; private OperatePanel userInterface; private JButton nextRecord, open; private int attributeNum; private String attributeNames[]; private ItemAttribute attributeArray[]; // Constructor -- initialize the Frame public ReadFile() { super( "Reading a Sequential File of Records" ); getContentPane().setLayout( new BorderLayout() ); JOptionPane.showMessageDialog( null, "First choose the Format-File"); OpenFormatFile fget = new OpenFormatFile(); attributeNum = fget.getAttributeNum(); attributeArray = new ItemAttribute[ attributeNum ]; attributeArray = fget.getAttribute(); attributeNames = new String[ attributeNum ]; for( int i = 0; i < attributeNum; i++ ) attributeNames[ i ] = attributeArray[ i ].getName(); userInterface = new OperatePanel( attributeNum, attributeNames ); nextRecord = userInterface.getDoTask1(); nextRecord.setText( "Show Record" ); nextRecord.setEnabled( false ); nextRecord.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { readRecord(); nextRecord.setText( "Next Record" ); } } ); addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { if( input != null) closeFile(); } } ); open = userInterface.getDoTask2(); open.setText( "Open Data-File" ); open.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { openFile(); } } ); getContentPane().add( userInterface, BorderLayout.CENTER ); pack(); setSize( 300, 200 ); show(); } private void openFile() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); int result = fileChooser.showOpenDialog( this ); // user clicked Cancel button on dialog 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 { // Open the file try { input = new ObjectInputStream( new FileInputStream( fileName ) ); open.setEnabled( false ); nextRecord.setEnabled( true ); } catch ( IOException e ) { JOptionPane.showMessageDialog( this, "Error Opening File", "Error", JOptionPane.ERROR_MESSAGE ); } } } public void readRecord() { GoodsRecord record; // input the values from the file try { record = ( GoodsRecord ) input.readObject(); GoodsField fieldArray[] = new GoodsField[ attributeNum ]; fieldArray = record.getGoodsRecord(); String values[] = new String[ attributeNum ]; for( int i =0; i < attributeNum; i++) { values[ i ] = fieldArray[ i ].getValue(); } userInterface.setFieldValues( values ); } catch ( EOFException eofex ) { nextRecord.setEnabled( false ); JOptionPane.showMessageDialog( this, "No more records in file", "End of File", JOptionPane.ERROR_MESSAGE ); } catch ( ClassNotFoundException cnfex ) { JOptionPane.showMessageDialog( this, "Unable to create object", "Class Not Found", JOptionPane.ERROR_MESSAGE ); } catch ( IOException ioex ) { JOptionPane.showMessageDialog( this, "Error during read from file", "Read Error", JOptionPane.ERROR_MESSAGE ); } } private void closeFile() { try { input.close(); //System.exit( 0 ); } catch ( IOException e ) { JOptionPane.showMessageDialog( this, "Error closing file", "Error", JOptionPane.ERROR_MESSAGE ); System.exit( 1 ); } } //public static void main( String args[] ) //{ //new ReadFile(); //}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -