Archive for October, 2007

Anonymous web server - Chapter 16 Files and Streams 925 177 }

Thursday, October 11th, 2007

Chapter 16 Files and Streams 925 177 } 178 179 // process exception from closing file 180 catch ( IOException ioException ) { 181 JOptionPane.showMessageDialog( this, 182 “Error closing file”, 183 “Error”, JOptionPane.ERROR_MESSAGE ); 184 185 System.exit( 1 ); 186 } 187 } 188 189 // read records from file and display only records of 190 // appropriate type 191 private void readRecords() 192 { 193 AccountRecord record; 194 DecimalFormat twoDigits = new DecimalFormat( “0.00″ ); 195 openFile( false ); 196 197 // read records 198 try { 199 recordDisplayArea.setText( “The accounts are:n” ); 200 201 // input the values from the file 202 while ( true ) { 203 204 // read one AccountRecord 205 record = ( AccountRecord ) input.readObject(); 206 207 // if proper acount type, display record 208 if ( shouldDisplay( record.getBalance() ) ) 209 recordDisplayArea.append( record.getAccount() + 210 “t” + record.getFirstName() + “t” + 211 record.getLastName() + “t” + 212 twoDigits.format( record.getBalance() ) + 213 “n” ); 214 } 215 } 216 217 // close file when end-of-file reached 218 catch ( EOFException eofException ) { 219 closeFile(); 220 } 221 222 // display error if cannot read object 223 // because class not found 224 catch ( ClassNotFoundException classNotFound ) { 225 JOptionPane.showMessageDialog( this, 226 “Unable to create object”, 227 “Class Not Found”, JOptionPane.ERROR_MESSAGE ); 228 } 229 Fig. 16.9Credit inquiry program (part 5 of 7). Fig. 16.9
Check Tomcat Web Hosting services for best quality webspace to host your web application.

924 Files and Streams Chapter 16 124 fileChooser.setFileSelectionMode( (Web design service)

Wednesday, October 10th, 2007

924 Files and Streams Chapter 16 124 fileChooser.setFileSelectionMode( 125 JFileChooser.FILES_ONLY ); 126 127 int result = fileChooser.showOpenDialog( this ); 128 129 // if user clicked Cancel button on dialog, return 130 if ( result == JFileChooser.CANCEL_OPTION ) 131 return; 132 133 // obtain selected file 134 fileName = fileChooser.getSelectedFile(); 135 } 136 137 // display error if file name invalid 138 if ( fileName == null || 139 fileName.getName().equals( “” ) ) 140 JOptionPane.showMessageDialog( this, 141 “Invalid File Name”, “Invalid File Name”, 142 JOptionPane.ERROR_MESSAGE ); 143 144 else { 145 146 // open file 147 try { 148 149 // close file from previous operation 150 if ( input != null ) 151 input.close(); 152 153 fileInput = new FileInputStream( fileName ); 154 input = new ObjectInputStream( fileInput ); 155 openButton.setEnabled( false ); 156 creditButton.setEnabled( true ); 157 debitButton.setEnabled( true ); 158 zeroButton.setEnabled( true ); 159 } 160 161 // catch problems manipulating file 162 catch ( IOException ioException ) { 163 JOptionPane.showMessageDialog( this, 164 “File does not exist”, “Invalid File Name”, 165 JOptionPane.ERROR_MESSAGE ); 166 } 167 } 168 169 } // end method openFile 170 171 // close file before application terminates 172 private void closeFile() 173 { 174 // close file 175 try { 176 input.close(); Fig. 16.9Credit inquiry program (part 4 of 7). Fig. 16.9
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Chapter 16 Files and Streams 923 71 72

Wednesday, October 10th, 2007

Chapter 16 Files and Streams 923 71 72 // create and configure button to get 73 // accounts with credit balances 74 zeroButton = new JButton( “Zero balances” ); 75 buttonPanel.add( zeroButton ); 76 zeroButton.addActionListener( new ButtonHandler() ); 77 78 // set up display area 79 recordDisplayArea = new JTextArea(); 80 JScrollPane scroller = 81 new JScrollPane( recordDisplayArea ); 82 83 // attach components to content pane 84 container.add( scroller, BorderLayout.CENTER ); 85 container.add( buttonPanel, BorderLayout.SOUTH ); 86 87 // disable creditButton, debitButton and zeroButton 88 creditButton.setEnabled( false ); 89 debitButton.setEnabled( false ); 90 zeroButton.setEnabled( false ); 91 92 // register window listener 93 addWindowListener( 94 95 // anonymous inner class for windowClosing event 96 new WindowAdapter() { 97 98 // close file and terminate program 99 public void windowClosing( WindowEvent event ) 100 { 101 closeFile(); 102 System.exit( 0 ); 103 } 104 105 } // end anonymous inner class 106 107 ); // end call to addWindowListener 108 109 // pack components and display window 110 pack(); 111 setSize( 600, 250 ); 112 show(); 113 114 } // end CreditInquiry constructor 115 116 // enable user to choose file to open first time; 117 // otherwise, reopen chosen file 118 private void openFile( boolean firstTime ) 119 { 120 if ( firstTime ) { 121 122 // display dialog, so user can choose file 123 JFileChooser fileChooser = new JFileChooser(); Fig. 16.9Credit inquiry program (part 3 of 7). Fig. 16.9
Check Tomcat Web Hosting services for best quality webspace to host your web application.

922 Files and Streams Chapter 16 18 19 (Affordable web design)

Tuesday, October 9th, 2007

922 Files and Streams Chapter 16 18 19 public class CreditInquiry extends JFrame { 20 private JTextArea recordDisplayArea; 21 private JButton openButton, 22 creditButton, debitButton, zeroButton; 23 private JPanel buttonPanel; 24 25 private ObjectInputStream input; 26 private FileInputStream fileInput; 27 private File fileName; 28 private String accountType; 29 30 // set up GUI 31 public CreditInquiry() 32 { 33 super( “Credit Inquiry Program” ); 34 35 Container container = getContentPane(); 36 37 // set up panel for buttons 38 buttonPanel = new JPanel(); 39 40 // create and configure button to open file 41 openButton = new JButton( “Open File” ); 42 buttonPanel.add( openButton ); 43 44 // register openButton listener 45 openButton.addActionListener( 46 47 // anonymous inner class to handle openButton event 48 new ActionListener() { 49 50 // open file for processing 51 public void actionPerformed( ActionEvent event ) 52 { 53 openFile( true ); 54 } 55 56 } // end anonymous inner class 57 58 ); // end call to addActionListener 59 60 // create and configure button to get 61 // accounts with credit balances 62 creditButton = new JButton( “Credit balances” ); 63 buttonPanel.add( creditButton ); 64 creditButton.addActionListener( new ButtonHandler() ); 65 66 // create and configure button to get 67 // accounts with debit balances 68 debitButton = new JButton( “Debit balances” ); 69 buttonPanel.add( debitButton ); 70 debitButton.addActionListener( new ButtonHandler() ); Fig. 16.9Credit inquiry program (part 2 of 7). Fig. 16.9
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Chapter 16 Files and Streams 921 classes FileInputStream, (Sri lanka web server)

Tuesday, October 9th, 2007

Chapter 16 Files and Streams 921 classes FileInputStream, FileOutputStream, DataInputStream and DataOutputStreamand adds several other methods, including a seekthat repositions the file-position pointer (the byte number of the next byte in the file to be read or written) to any position in the file. However, class RandomAccessFile cannot read and write entire objects. Performance Tip 16.5 The process of closing and reopening a file for the purpose of positioning the file-position pointer back to the beginning of a file is a time-consuming task for the computer. If this is done frequently, it can slow the performance of your program. The program of Fig. 16.9 enables a credit manager to display the account information for those customers with zero balances (i.e., customers who do not owe the company any money), credit balances (i.e., customers to whom the company owes money) and debit balances (i.e., customers who owe the company money for goods and services received in the past). The program displays buttons that allow a credit manager to obtain credit information. The Credit balances button produces a list of accounts with credit balances. The Debit balances button produces a list of accounts with debit balances. The Zero balances button produces a list of accounts with zero balances. Records are displayed in a JTextAreacalled recordDisplayArea. The record information is collected by reading through the entire file and determining, for each record, whether it satisfies the criteria for the account type selected by the credit manager. Clicking one of the balance buttons sets variable accountType(line 274) to the clicked button s text (e.g., Zero balances) and invokes method readRecords(191 238), which loops through the file and reads every record. Line 208 of method readRecordscalls method shouldDisplay(241 259) to determine whether the current record satisfies the account type requested. If shouldDisplay returns true, the program appends the account information for the current record to the JTextArearecordDisplay. When the end- of-file marker is reached, line 219 calls method closeFileto close the file. 1 // Fig. 16.9: CreditInquiry.java 2 // This program reads a file sequentially and displays the 3 // contents in a text area based on the type of account the 4 // user requests (credit balance, debit balance or 5 // zero balance). 6 7 // Java core packages 8 import java.io.*; 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.text.DecimalFormat; 12 13 // Java extension packages 14 import javax.swing.*; 15 16 // Deitel packages 17 import com.deitel.jhtp4.ch16.AccountRecord; Fig. 16.9Credit inquiry program (part 1 of 7). Fig. 16.9
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

920 Files and Streams Chapter 16 Fig. 16.8Reading (Best web hosting)

Monday, October 8th, 2007

920 Files and Streams Chapter 16 Fig. 16.8Reading a sequential file (part 6 of 6). Fig. 16.8 Most of the code in this example is similar to Fig. 16.6, so we discuss only the key lines of code that are different. Line 105 calls JFileChoosermethod showOpenDialogto display the Open dialog (second screen capture in Fig. 16.8). The behavior and GUI are the same as the dialog displayed by showSaveDialog, except that the title of the dialog and the Save button are both replaced with Open. Lines 125 126 create a ObjectInputStreamobject and assign it to input. The FilefileNameis passed to the FileInputStreamconstructor to open the file. The program reads a record from the file each time the user clicks the Next Record button. Line 84 in Next Record s actionPerformed method calls method readRecord (lines 144 187) to read one record from the file. Line 150 calls method readObjectto read an Objectfrom the ObjectInputStream. To use AccountRecordspecific methods, we cast the returned Objectto type AccountRecord. If the end-of-file marker is reached during reading, readObjectthrows an EndOfFile- Exception. To retrieve data sequentially from a file, programs normally start reading from the beginning of the file and read all the data consecutively until the desired data are found. It might be necessary to process the file sequentially several times (from the beginning of the file) during the execution of a program. Class FileInputStreamdoes not provide the ability to reposition to the beginning of the file to read the file again unless the program closes the file and reopens it. Class RandomAccessFile objects can reposition to the beginning of the file. Class RandomAccessFile provides all the capabilities of the
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Web hosting e commerce - Chapter 16 Files and Streams 919 176 “Unable

Monday, October 8th, 2007

Chapter 16 Files and Streams 919 176 “Unable to create object”, 177 “Class Not Found”, JOptionPane.ERROR_MESSAGE ); 178 } 179 180 // display error message if cannot read 181 // due to problem with file 182 catch ( IOException ioException ) { 183 JOptionPane.showMessageDialog( this, 184 “Error during read from file”, 185 “Read Error”, JOptionPane.ERROR_MESSAGE ); 186 } 187 } 188 189 // close file and terminate application 190 private void closeFile() 191 { 192 // close file and exit 193 try { 194 input.close(); 195 System.exit( 0 ); 196 } 197 198 // process exception while closing file 199 catch ( IOException ioException ) { 200 JOptionPane.showMessageDialog( this, 201 “Error closing file”, 202 “Error”, JOptionPane.ERROR_MESSAGE ); 203 204 System.exit( 1 ); 205 } 206 } 207 208 // execute application; ReadSequentialFile constructor 209 // displays window 210 public static void main( String args[] ) 211 { 212 new ReadSequentialFile(); 213 } 214 215 } // end class ReadSequentialFile Fig. 16.8Reading a sequential file (part 5 of 6). Fig. 16.8
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

918 Files and Streams Chapter 16 123 // (Fedora web server)

Sunday, October 7th, 2007

918 Files and Streams Chapter 16 123 // open file 124 try { 125 input = new ObjectInputStream( 126 new FileInputStream( fileName ) ); 127 128 openButton.setEnabled( false ); 129 nextButton.setEnabled( true ); 130 } 131 132 // process exceptions opening file 133 catch ( IOException ioException ) { 134 JOptionPane.showMessageDialog( this, 135 “Error Opening File”, “Error”, 136 JOptionPane.ERROR_MESSAGE ); 137 } 138 139 } // end else 140 141 } // end method openFile 142 143 // read record from file 144 public void readRecord() 145 { 146 AccountRecord record; 147 148 // input the values from the file 149 try { 150 record = ( AccountRecord ) input.readObject(); 151 152 // create array of Strings to display in GUI 153 String values[] = { 154 String.valueOf( record.getAccount() ), 155 record.getFirstName(), 156 record.getLastName(), 157 String.valueOf( record.getBalance() ) }; 158 159 // display record contents 160 userInterface.setFieldValues( values ); 161 } 162 163 // display message when end-of-file reached 164 catch ( EOFException endOfFileException ) { 165 nextButton.setEnabled( false ); 166 167 JOptionPane.showMessageDialog( this, 168 “No more records in file”, 169 “End of File”, JOptionPane.ERROR_MESSAGE ); 170 } 171 172 // display error message if cannot read object 173 // because class not found 174 catch ( ClassNotFoundException classNotFoundException ) { 175 JOptionPane.showMessageDialog( this, Fig. 16.8Reading a sequential file (part 4 of 6). Fig. 16.8
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Chapter 16 Files and Streams 917 70 // (Cheapest web hosting)

Sunday, October 7th, 2007

Chapter 16 Files and Streams 917 70 // get reference to generic task button doTask2 from BankUI 71 nextButton = userInterface.getDoTask2Button(); 72 nextButton.setText( “Next Record” ); 73 nextButton.setEnabled( false ); 74 75 // register listener to call readRecord when button pressed 76 nextButton.addActionListener( 77 78 // anonymous inner class to handle nextRecord event 79 new ActionListener() { 80 81 // call readRecord when user clicks nextRecord 82 public void actionPerformed( ActionEvent event ) 83 { 84 readRecord(); 85 } 86 87 } // end anonymous inner class 88 89 ); // end call to addActionListener 90 91 pack(); 92 setSize( 300, 200 ); 93 show(); 94 95 } // end ReadSequentialFile constructor 96 97 // enable user to select file to open 98 private void openFile() 99 { 100 // display file dialog so user can select file to open 101 JFileChooser fileChooser = new JFileChooser(); 102 fileChooser.setFileSelectionMode( 103 JFileChooser.FILES_ONLY ); 104 105 int result = fileChooser.showOpenDialog( this ); 106 107 // if user clicked Cancel button on dialog, return 108 if ( result == JFileChooser.CANCEL_OPTION ) 109 return; 110 111 // obtain selected file 112 File fileName = fileChooser.getSelectedFile(); 113 114 // display error if file name invalid 115 if ( fileName == null || 116 fileName.getName().equals( “” ) ) 117 JOptionPane.showMessageDialog( this, 118 “Invalid File Name”, “Invalid File Name”, 119 JOptionPane.ERROR_MESSAGE ); 120 121 else { 122 Fig. 16.8Reading a sequential file (part 3 of 6). Fig. 16.8
We recommend high quality webhost to host and run your jsp application: christian web host services.

Web server on xp - 916 Files and Streams Chapter 16 18 private

Saturday, October 6th, 2007

916 Files and Streams Chapter 16 18 private BankUI userInterface; 19 private JButton nextButton, openButton; 20 21 // Constructor –initialize the Frame 22 public ReadSequentialFile() 23 { 24 super( “Reading a Sequential File of Objects” ); 25 26 // create instance of reusable user interface 27 userInterface = new BankUI( 4 ); // four textfields 28 getContentPane().add( 29 userInterface, BorderLayout.CENTER ); 30 31 // get reference to generic task button doTask1 from BankUI 32 openButton = userInterface.getDoTask1Button(); 33 openButton.setText( “Open File” ); 34 35 // register listener to call openFile when button pressed 36 openButton.addActionListener( 37 38 // anonymous inner class to handle openButton event 39 new ActionListener() { 40 41 // close file and terminate application 42 public void actionPerformed( ActionEvent event ) 43 { 44 openFile(); 45 } 46 47 } // end anonymous inner class 48 49 ); // end call to addActionListener 50 51 // register window listener for window closing event 52 addWindowListener( 53 54 // anonymous inner class to handle windowClosing event 55 new WindowAdapter() { 56 57 // close file and terminate application 58 public void windowClosing( WindowEvent event ) 59 { 60 if ( input != null ) 61 closeFile(); 62 63 System.exit( 0 ); 64 } 65 66 } // end anonymous inner class 67 68 ); // end call to addWindowListener 69 Fig. 16.8Fig. 16.8 Reading a sequential file (part 2 of 6).
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.