1 // File: CreateSequentialFile.java (Fig. 15.4 in JHTP2) 2 // This program uses TextFields to get information from the 3 // user at the keyboard and writes the information to a 4 // sequential file. 5 6 import java.io.*; 7 import java.awt.*; 8 import java.awt.event.*; 9 10 public class CreateSequentialFile extends Frame 11 implements ActionListener { 12 13 // TextFields where user enters account number, first name, 14 // last name and balance. 15 private TextField accountField, firstNameField, 16 lastNameField, balanceField; 17 18 private Button enter, // send record to file 19 done; // quit program 20 21 // Application other pieces 22 private DataOutputStream output; 23 24 public CreateSequentialFile() 25 { 26 super( "Create Client File" ); 27 28 // Open the file 29 try { 30 output = new DataOutputStream( 31 new FileOutputStream( "client.dat" ) ); 32 } 33 catch ( IOException e ) { 34 System.err.println( "File not opened properly\n" + 35 e.toString() ); 36 System.exit( 1 ); 37 } 38 39 setSize( 300, 150 ); 40 setLayout( new GridLayout( 5, 2 ) ); 41 42 // create the components of the Frame 43 add( new Label( "Account Number" ) ); 44 accountField = new TextField(); 45 add( accountField ); 46 47 add( new Label( "First Name" ) ); 48 firstNameField = new TextField( 20 ); 49 add( firstNameField ); 50 51 add( new Label( "Last Name" ) ); 52 lastNameField = new TextField( 20 ); 53 add( lastNameField ); 54 55 add( new Label( "Balance" ) ); 56 balanceField = new TextField( 20 ); 57 add( balanceField ); 58 59 enter = new Button( "Enter" ); 60 enter.addActionListener( this ); 61 add( enter ); 62 63 done = new Button( "Done" ); 64 done.addActionListener( this ); 65 add( done ); 66 67 setVisible( true ); 68 } 69 70 public void addRecord() 71 { 72 int accountNumber = 0; 73 Double d; 74 75 if ( ! accountField.getText().equals( "" ) ) { 76 77 // output the values to the file 78 try { 79 accountNumber = 80 Integer.parseInt( accountField.getText() ); 81 82 if ( accountNumber > 0 ) { 83 output.writeInt( accountNumber ); 84 output.writeUTF( firstNameField.getText() ); 85 output.writeUTF( lastNameField.getText() ); 86 d = new Double ( balanceField.getText() ); 87 output.writeDouble( d.doubleValue() ); 88 } 89 90 // clear the TextFields 91 accountField.setText( "" ); 92 firstNameField.setText( "" ); 93 lastNameField.setText( "" ); 94 balanceField.setText( "" ); 95 } 96 catch ( NumberFormatException nfe ) { 97 System.err.println( 98 "You must enter an integer account number" ); 99 } 100 catch ( IOException io ) { 101 System.err.println( 102 "Error during write to file\n" + 103 io.toString() ); 104 System.exit( 1 ); 105 } 106 } 107 } 108 109 public void actionPerformed( ActionEvent e ) 110 { 111 addRecord(); 112 113 if ( e.getSource() == done ) { 114 try { 115 output.close(); 116 } 117 catch ( IOException io ) { 118 System.err.println( "File not closed properly\n" + 119 io.toString() ); 120 } 121 122 System.exit( 0 ); 123 } 124 } 125 126 public static void main( String args[] ) 127 { 128 new CreateSequentialFile(); 129 } 130 } 131