26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

918 Files and Streams Chapter 16<br />

123 // open file<br />

124 try {<br />

125 input = new ObjectInputStream(<br />

126 new FileInputStream( fileName ) );<br />

127<br />

128 openBut<strong>to</strong>n.setEnabled( false );<br />

129 nextBut<strong>to</strong>n.setEnabled( true );<br />

130 }<br />

131<br />

132 // process exceptions opening file<br />

133 catch ( IOException ioException ) {<br />

134 JOptionPane.showMessageDialog( this,<br />

135 "Error Opening File", "Error",<br />

136 JOptionPane.ERROR_MESSAGE );<br />

137 }<br />

138<br />

139 } // end else<br />

140<br />

141 } // end method openFile<br />

142<br />

143 // read record from file<br />

144 public void readRecord()<br />

145 {<br />

146 AccountRecord record;<br />

147<br />

148 // input the values from the file<br />

149 try {<br />

150 record = ( AccountRecord ) input.readObject();<br />

151<br />

152 // create array of Strings <strong>to</strong> display in GUI<br />

153 String values[] = {<br />

154 String.valueOf( record.getAccount() ),<br />

155 record.getFirstName(),<br />

156 record.getLastName(),<br />

157 String.valueOf( record.getBalance() ) };<br />

158<br />

159 // display record contents<br />

160 userInterface.setFieldValues( values );<br />

161 }<br />

162<br />

163 // display message when end-of-file reached<br />

164 catch ( EOFException endOfFileException ) {<br />

165 nextBut<strong>to</strong>n.setEnabled( false );<br />

166<br />

167 JOptionPane.showMessageDialog( this,<br />

168 "No more records in file",<br />

169 "End of File", JOptionPane.ERROR_MESSAGE );<br />

170 }<br />

171<br />

172 // display error message if cannot read object<br />

173 // because class not found<br />

174 catch ( ClassNotFoundException classNotFoundException ) {<br />

175 JOptionPane.showMessageDialog( this,<br />

Fig. Fig. 16.8 16.8 Reading a sequential file (part 4 of 6).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!