Chapter 16 Files and Streams 915 In the (Photography web hosting)
Saturday, October 6th, 2007Chapter 16 Files and Streams 915 In the sample execution for the program of Fig. 16.6, we entered information for five accounts (see Fig. 16.7). The program does not show how the data records actually appear in the file. To verify that the file has been created successfully, in the next section we create a program to read the file. 16.5 Reading Data from a Sequential-Access File Data are stored in files so that they may be retrieved for processing when needed. The previous section demonstrated how to create a file for sequential access. In this section, we discuss how to read data sequentially from a file. The program of Fig. 16.8 reads records from a file created by the program of Fig. 16.6 and displays the contents of the records. The program opens the file for input by creating a FileInputStream object. The program specifies the name of the file to open as an argument to the FileInputStream constructor. In Fig. 16.6, we wrote objects to the file, using an ObjectOutputStreamobject. Data must be read from the file in the same format in which it was written to the file. Therefore, we use an ObjectInputStream chained to a FileInputStreamin this program. Note that the third sample screen capture shows the GUI displaying the last record in the file. Sample Data 100 Bob Jones 24.98 200 Steve Doe -345.67 300 Pam White 0.00 400 Sam Stone -42.16 500 Sue Rich 224.62 Fig. 16.7Sample data for the program of Fig. 16.6. 16. 1 // Fig. 16.8: ReadSequentialFile.java 2 // This program reads a file of objects sequentially 3 // and displays each record. 4 5 // Java core packages 6 import java.io.*; 7 import java.awt.*; 8 import java.awt.event.*; 9 10 // Java extension packages 11 import javax.swing.*; 12 13 // Deitel packages 14 import com.deitel.jhtp4.ch16.*; 15 16 public class ReadSequentialFile extends JFrame { 17 private ObjectInputStream input; Fig. 16.8Reading a sequential file (part 1 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.