-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadSequentialFile.java
More file actions
237 lines (181 loc) · 6.96 KB
/
Copy pathReadSequentialFile.java
File metadata and controls
237 lines (181 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// This program reads a file of objects sequentially
// and displays each record.
// Java core packages
import java.io.*;
import java.awt.*;
import java.awt.event.*;
// Java extension packages
import javax.swing.*;
@SuppressWarnings("serial")
public class ReadSequentialFile extends JFrame {
private static final long serialVersionUID = 3417040053089159204L;
private ObjectInputStream input;
private BankUI userInterface;
private JButton nextButton, openButton;
// Constructor -- initialize the Frame
public ReadSequentialFile()
{
super( "Reading a Sequential File of Objects" );
// create instance of reusable user interface
userInterface = new BankUI(8);
getContentPane().add(
userInterface, BorderLayout.CENTER );
for(int x = 0; x < 3; x++){
userInterface.fields[x].setEditable( false );
userInterface.fields[x].setBackground(Color.white);
}
// get reference to generic task button doTask1 from BankUI
openButton = userInterface.getDoTask1Button();
openButton.setText( "Open File" );
openButton.setMnemonic('o');
// register listener to call openFile when button pressed
openButton.addActionListener(
// anonymous inner class to handle openButton event
new ActionListener() {
// close file and terminate application
public void actionPerformed( ActionEvent event )
{
openFile();
}
} // end anonymous inner class
); // end call to addActionListener
// get reference to generic task button doTask2 from BankUI
nextButton = userInterface.getDoTask2Button();
nextButton.setText( "Next Record" );
nextButton.setEnabled( false );
nextButton.setMnemonic('N');
// register listener to call readRecord when button pressed
nextButton.addActionListener(
// anonymous inner class to handle nextRecord event
new ActionListener() {
// call readRecord when user clicks nextRecord
public void actionPerformed( ActionEvent event )
{
readRecord();
}
} // end anonymous inner class
); // end call to addActionListener
nextButton.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
readRecord();
} // keyPressed
}/*KeyAdapter*/);
pack(); // pack the window, making it just big enough
// configure window
setDefaultCloseOperation(
WindowConstants.EXIT_ON_CLOSE );
// Get the size of the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (((screenSize.width - this.getWidth()) / 2) / 2);
int y = (((screenSize.height - this.getHeight()) / 2) / 2);
this.setLocation(x, y);
// Set font style
Font myFont = new Font("Franklin", Font.BOLD, 14);
//produce the frame and show its
setSize( 500, 350 );
setResizable(false);
setVisible( true );
} // end ReadSequentialFile constructor
// enable user to select file to open
private void openFile()
{
// display file dialog so user can select file to open
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = fileChooser.showOpenDialog( this );
// if user clicked Cancel button on dialog, return
if ( result == JFileChooser.CANCEL_OPTION )
return;
// obtain selected file
File fileName = fileChooser.getSelectedFile();
// display error if file name invalid
if ( fileName == null ||
fileName.getName().equals( "" ) )
JOptionPane.showMessageDialog( this,
"Invalid File Name", "Invalid File Name",
JOptionPane.ERROR_MESSAGE );
else {
// open file
try {
input = new ObjectInputStream(
new FileInputStream( fileName ) );
openButton.setEnabled( false );
nextButton.setEnabled( true );
nextButton.requestFocus();
}
// process exceptions opening file
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"Error Opening File", "Error",
JOptionPane.ERROR_MESSAGE );
}
} // end else
} // end method openFile
// read record from file
public void readRecord()
{
AccountRecord record;
// input the values from the file
try {
record = ( AccountRecord ) input.readObject(); // going from one type to the other type
// create array of Strings to display in GUI
String values[] = {
String.valueOf( record.getAccount() ),
record.getFirstName(),
record.getLastName(),
record.getAddress(),
record.getSocsec(),
String.valueOf(record.getBalance()),
String.valueOf(record.getGpa()),
record.getTitle()
};
// display record contents
userInterface.setFieldValues( values );
}
// display message when end-of-file reached
catch ( EOFException endOfFileException ) {
nextButton.setEnabled( false );
JOptionPane.showMessageDialog( this,
"No more records in file",
"End of File", JOptionPane.ERROR_MESSAGE );
}
// display error message if cannot read object
// because class not found
catch ( ClassNotFoundException classNotFoundException ) {
JOptionPane.showMessageDialog( this,
"Unable to create object",
"Class Not Found", JOptionPane.ERROR_MESSAGE );
}
// display error message if cannot read
// due to problem with file
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"Error during read from file",
"Read Error", JOptionPane.ERROR_MESSAGE );
}
}
// close file and terminate application
private void closeFile()
{
// close file and exit
try {
input.close();
System.exit( 0 );
}
// process exception while closing file
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this,
"Error closing file",
"Error", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
}
// execute application; ReadSequentialFile constructor
// displays window
public static void main( String args[] )
{
new ReadSequentialFile();
}
} // end class ReadSequentialFile