Chapter 16 Files and Streams 903 Class FileReader (Web hosting company)
Chapter 16 Files and Streams 903 Class FileReader (a subclass of InputStreamReader) and class File- Writer (a subclass of OutputStreamWriter) read characters from and write characters to a file, respectively. Class PipedReader and class PipedWriter implement piped-character streams that can be used to transfer information between threads. Class StringReader and StringWriter read and write characters to Strings. A PrintWriter writes characters to a stream. Class File enables programs to obtain information about a file or directory. We discuss class File extensively in Section 16.12. 16.4 Creating a Sequential-Access File Java imposes no structure on a file. Notions like record do not exist in Java files. Therefore, the programmer must structure files to meet the requirements of applications. In the following example, we see how the programmer can impose a simple record structure on a file. First we present the program, then we analyze it in detail. The program of Fig. 16.4 Fig. 16.6 creates a simple sequential-access file that might be used in an accounts receivable system to help manage the money owed by a company s credit clients. For each client, the program obtains an account number, the client s first name, the client s last name and the client s balance (i.e., the amount the client still owes the company for goods and services received in the past). The data obtained for each client constitutes a record for that client. The program uses the account number as the record key; that is, the file will be created and maintained in account-number order. [Note: This program assumes the user enters the records in account-number order. In a comprehensive accounts receivable system, a sorting capability would be provided so the user could enter the records in any order the records would then be sorted and written to the file.] Most of the programs in this chapter have a similar GUI, so this program defines class BankUI (Fig. 16.4) to encapsulate the GUI. (See the second sample output screen in Fig. 16.6.) Also, the program defines class AccountRecord (Fig. 16.5) to encapsulate the client record information (i.e., account, first name, etc.) used by the examples in this chapter. For reuse, classes BankUI and AccountRecord are defined in package com.deitel.jhtp4.ch16. [Note: Most of the programs in this chapter use classes BankUI and Account- Record. When you compile these classes, or any others that will be reused in this chapter, you should place the classes in a common directory. When you compile classes that use BankUI and AccountRecord, be sure to specify the -classpath command line argument to both javac and java, as in javac -classpath .;packageLocation ClassName.java java -classpath .;packageLocation ClassName where packageLocation represents the common directory in which the classes of the package com.deitel.jhtp4.ch16 reside and ClassName represents the class to compile or execute. Be sure to include the current directory (specified with .) in the class path. [Note: If your packaged classes are in a JAR file, the packageLocation should include the location and name of the actual JAR file.] Also, the path separator shown (;, which is used in Microsoft Windows) should be appropriate for your platform (such as : on UNIX/Linux).] Class BankUI (Fig. 16.4) contains two JButtons and arrays of JLabels and JTextFields. The number of JLabels and JTextFields is set with the constructor
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.