Archive for September, 2007

Chapter 16 Files and Streams 903 Class FileReader (Web hosting company)

Sunday, September 30th, 2007

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.

902 Files and Streams Chapter 16 back onto

Sunday, September 30th, 2007

902 Files and Streams Chapter 16 back onto the stream. PushbackInputStreams are used by programs (like compilers) that parse their inputs that is, break them into meaningful units (such as the keywords, identifiers and operators that the compiler must recognize). When object instance variables are output to a disk file, in a sense we lose the object s type information. We have only data, not type information, on a disk. If the program that is going to read this data knows what object type it corresponds to, then the data is simply read into objects of that type. Sometimes, we would like to read or write an entire object to a file. The ObjectInputStream and ObjectOutputStream classes, which respectively implement the ObjectInput and ObjectOutput interfaces, enable an entire object to be read from or written to a file (or other stream type). We often chain ObjectInput- Streams to FileInputStreams. (We also chain ObjectOutputStreams to File- OutputStreams.) The ObjectOutput interface contains method writeObject, which takes an Object that implements interface Serializable as an argument and writes its information to the OutputStream. Correspondingly, the ObjectInput interface requires method readObject, which reads and returns an Object from an Input- Stream. After the reading of an object, it can be cast to the desired type. Additionally, these interfaces include other Object-centric methods as well as the same methods as DataInput and DataOutputfor reading and writing primitive data types. Java stream I/O includes capabilities for inputting from byte arrays in memory and outputting to byte arrays in memory. A ByteArrayInputStream (a subclass of InputStream) reads from a byte array in memory. A ByteArrayOutputStream (a subclass of OutputStream) outputs to a byte array in memory. One application of byte-array I/O is data validation. A program can input an entire line at a time from the input stream into a byte array. Then, a validation routine can scrutinize the contents of the byte array and correct the data, if necessary. Then, the program can proceed to input from the byte array, knowing that the input data is in the proper format. Outputting to a byte array is a nice way to take advantage of the powerful output-formatting capabilities of Java streams. For example, data can be prepared in a bytearray, using the same formatting that will be displayed at a later time, then output to a disk file to preserve the screen image. A SequenceInputStream(a subclass of InputStream) enables concatenation of several InputStreams, so that the program sees the group as one continuous Input- Stream. As the program reaches the end of an input stream, that stream closes and the next stream in the sequence opens. In addition to the byte based streams, Java provides Reader and Writer classes, which are Unicode, two-byte, character based streams. Most of the byte-based streams have corresponding character-based Reader or Writer classes. Class BufferedReader (a subclass of abstract class Reader) and class BufferedWriter (a subclass of abstract class Writer) enable efficient buffering for character-based streams. Character-based streams use Unicode characters such streams can process data in any language that the Unicode character set represents. Class CharArrayReader and class CharArrayWriterread and write a stream of characters to a character array. A PushbackReader (a subclass of abstract class FilterReader) enables characters to be pushed back on a character stream. A LineNumberReader (a subclass of BufferedReader) is a buffered character-stream that keeps track of line numbers (i.e., a newline, a return or a carriage-return line-feed combination).
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Chapter 16 Files and Streams 901 The DataInput (Web server type)

Sunday, September 30th, 2007

Chapter 16 Files and Streams 901 The DataInput interface is implemented by class DataInputStream and class RandomAccessFile (discussed later in the chapter); each needs to read primitive data types from a stream. DataInputStreams enable a program to read binary data from an InputStream. The DataInput interface includes methods read (for byte arrays), readBoolean, readByte, readChar, readDouble, readFloat, readFully (for byte arrays), readInt, readLong, readShort, readUnsignedByte, readUnsignedShort, readUTF (for strings) and skipBytes. The DataOutput interface is implemented by class DataOutputStream (a subclass of FilterOutputStream) and class RandomAccessFile; each needs to write primitive data types to an OutputStream. DataOutputStreams enable a program to write binary data to an OutputStream. The DataOutput interface includes methods flush, size, write (for a byte), write (for a byte array), writeBoolean, writeByte, writeBytes, writeChar, writeChars (for Unicode Strings), writeDouble, writeFloat, writeInt, writeLong, writeShort and writeUTF. Buffering is an I/O-performance-enhancement technique. With a BufferedOutputStream (a subclass of class FilterOutputStream), each output statement does not necessarily result in an actual physical transfer of data to the output device. Rather, each output operation is directed to a region in memory called a buffer that is large enough to hold the data of many output operations. Then, actual transfer to the output device is performed in one large physical output operation each time the buffer fills. The output operations directed to the output buffer in memory are often called logical output operations. With a BufferedOutputStream, a partially filled buffer can be forced out to the device at any time by invoking the stream object s flush method. Performance Tip 16.1 Since typical physical output operations are extremely slow compared to the speed of accessing computer memory, buffered outputs normally yield significant performance improvements over unbuffered outputs. With a BufferedInputStream (a subclass of class FilterInputStream), many logical chunks of data from a file are read as one large physical input operation into a memory buffer. As a program requests each new chunk of data, it is taken from the buffer (this is sometimes referred to as a logical input operation). When the buffer is empty, the next actual physical input operation from the input device is performed to read in the next group of logical chunks of data. Thus, the number of actual physical input operations is small compared with the number of read requests issued by the program. Performance Tip 16.2 Since typical input operations are extremely slow compared to the speed of accessing computer memory, buffered inputs normally yield significant performance improvements over unbuffered inputs. A PushbackInputStream (a subclass of class FilterInputStream) is for applications more exotic than those most programmers require. Essentially, the application reading a PushbackInputStream reads bytes from the stream and forms aggregates consisting of several bytes. Sometimes, to determine that one aggregate is complete, the application must read the first character past the end of the first aggregate. Once the program determines that the current aggregate is complete, the extra character is pushed
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

900 Files and Streams Chapter 16 A portion

Saturday, September 29th, 2007

900 Files and Streams Chapter 16 A portion of the class hierarchy of the java.iopackage Writer BufferedWriter CharArrayWriter FilterWriter OutputStreamWriter FileWriter PipedWriter PrintWriter StringWriter Fig. 16.3A portion of the class hierarchy of the java.iopackage (part 2 of 2). 16. InputStream and OutputStream (subclasses of Object) are abstract classes that define methods for performing byte-based input and output, respectively. Programs perform byte-based file input/output with FileInputStream(a subclass of InputStream) and FileOutputStream(a subclass of OutputStream). We use these classes extensively in the examples in this chapter. Pipes are synchronized communication channels between threads or processes. Java provides PipedOutputStream(a subclass of OutputStream) and PipedInput- Stream (a subclass of InputStream) to establish pipes between two threads. One thread sends data to another by writing to a PipedOutputStream. The target thread reads information from the pipe via a PipedInputStream. A PrintStream(a subclass of FilterOutputStream) performs text output to the specified stream. Actually, we have been using PrintStreamoutput throughout the text to this point System.outis a PrintStream, as is System.err. A FilterInputStreamfilters an InputStream, and a FilterOutStreamfilters an OutputStream; filtering simply means that the filter stream provides additional functionality, such as buffering, monitoring line numbers or aggregating data bytes into meaningful primitive-data-type units. FilterInputStream and FilterOutput- Streamare abstractclasses, so additional functionality is provided by their subclasses. Reading data as raw bytes is fast but crude. Usually programs read data as aggregates of bytes that form an int, a float, a doubleand so on. Java programs can use several classes to input and output data in aggregate form. A RandomAccessFileis useful for direct-access applications, such as transaction- processing applications like airline-reservations systems and point-of-sale systems. With a sequential-access file, each successive input/output request reads or writes the next consecutive set of data in the file. With a random-access file, each successive input/output request could be directed to any part of the file perhaps one widely separated from the part of the file referenced in the previous request. Direct-access applications provide rapid access to specific data items in large files; often, such applications are used in applications that require users to wait for answers these answers must be made available quickly, or the people might become impatient and take their business elsewhere.
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Chapter 16 Files and Streams 899 A portion

Saturday, September 29th, 2007

Chapter 16 Files and Streams 899 A portion of the class hierarchy of the java.iopackage java.lang.Object File FileDescriptor InputStream ByteArrayInputStream FileInputStream FilterInputStream BufferedInputStream DataInputStream PushbackInputStream ObjectInputStream PipedInputStream SequenceInputStream OutputStream ByteArrayOutputStream FileOutputStream FilterOutputStream BufferedOutputStream DataOutputStream PrintStream ObjectOutputStream PipedOutputStream RandomAccessFile Reader BufferedReader LineNumberReader CharArrayReader FilterReader PushbackReader InputStreamReader FileReader PipedReader StringReader Fig. 16.3A portion of the class hierarchy of the java.iopackage (part 1 of 2). 16.
You want to have a cheap webhost for your apache application, then check apache web hosting services.

898 Files and Streams Chapter 16 ing platform

Friday, September 28th, 2007

898 Files and Streams Chapter 16 ing platform represents files or streams. In some cases, the end-of-file indication occurs as an exception. In other cases, the indication is a return value from a method invoked on a stream-processing object. We demonstrate both cases in this chapter. A Java program opens a file by creating an object and associating a stream of bytes with the object. Java also can associates streams of bytes associated with devices. In fact, Java creates three stream objects that are associated with devices when a Java program begins executing System.in, System.outand System.err. The streams associated with these objects provide communication channels between a program and a particular device. For example, object System.in(the standard input stream object) normally enables a program to input bytes from the keyboard, object System.out (the standard output stream object) normally enables a program to output data to the screen and object System.err (the standard error stream object) normally enables a program to output error messages to the screen. Each of these streams can be redirected. For System.in, this enables the program to read bytes from a different source. For System.out and System.err, this enables the output to be sent to a different location, such as a file on disk. Class Systemprovides methods setIn, setOutand setErrto redirect the standard input, output and error streams. Java programs perform file processing by using classes from package java.io. This package includes definitions for the stream classes, such as FileInputStream (for byte-based input from a file), FileOutputStream (for byte-based output to a file), FileReader (for character-based input from a file) and FileWriter (for character- based output to a file). Files are opened by creating objects of these stream classes that inherit from classes InputStream, OutputStream, Reader and Writer, respectively. Thus, the methods of these stream classes can all be applied to file streams as well. To perform input and output of data types, objects of class ObjectInputStream, DataInputStream, ObjectOutputStream and DataOutputStream will be used together with the byte-based file stream classes FileInputStream and File- OutputStream. Figure 16.3 summarizes the inheritance relationships of many of the Java I/O classes (abstract classes are shown in italic font). The following discussion overviews the capabilities of each of the classes in Fig. 16.3. Java offers many classes for performing input/output. This section briefly overviews many of these classes and explains how they relate to one another. In the rest of the chapter, we use several of these stream classes as we implement a variety of file-processing programs that create, manipulate and destroy sequential-access files and random-access files. We also include a detailed example on class File, which is useful for obtaining information about files and directories. In Chapter 17, Networking, we use stream classes extensively to implement networking applications. 0 1 2 3 4 5 6 7 8 9 … n-1… end-of-file marker Fig. 16.2Java s view of a file of n bytes. 16.
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Chapter 16 Files and Streams (Ipower web hosting) 897 Randy Red

Friday, September 28th, 2007

Chapter 16 Files and Streams 897 Randy Red 01001010 J u d y Judy Green Sally Black Tom Blue Judy Green Iris Orange File Record Field Byte (ASCII character J) 1 Bit Fig. 16.1The data hierarchy. 16. To facilitate the retrieval of specific records from a file, at least one field in each record is chosen as a record key. A record key identifies a record as belonging to a particular person or entity that is unique from all other records. In the payroll record described previously, the employee identification number normally would be chosen as the record key. There are many ways to organize records in a file. The most common organization is called a sequential file in which records are stored in order by the record-key field. In a payroll file, records are placed in order by employee identification number. The first employee record in the file contains the lowest employee identification number, and subsequent records contain increasingly higher employee identification numbers. Most businesses store data in many different files. For example, companies might have payroll files, accounts receivable files (listing money due from clients), accounts payable files (listing money due to suppliers), inventory files (listing facts about all the items handled by the business) and many other file types. Often, a group of related files is called a database. A collection of programs designed to create and manage databases is called a database management system (DBMS). 16.3 Files and Streams Java views each file as a sequential stream of bytes (Fig. 16.2). Each file ends either with an end-of-file marker or at a specific byte number recorded in a system-maintained administrative data structure. Java abstracts this concept from the programmer. A Java program processing a stream of bytes simply receives an indication from the system when the program reaches the end of the stream the program does not need to know how the underly
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

896 Files and Streams Chapter 16 impressive functions (Vps web hosting)

Thursday, September 27th, 2007

896 Files and Streams Chapter 16 impressive functions performed by computers involve only the most fundamental manipulations of 0s and 1s. The smallest data item in a computer can assume the value 0 or the value 1. Such a data item is called a bit (short for binary digit a digit that can assume one of two values). Computer circuitry performs various simple bit manipulations, such as examining the value of a bit, setting the value of a bit and reversing a bit (from 1to 0 or from 0to 1). It is cumbersome for programmers to work with data in the low-level form of bits. Instead, programmers prefer to work with data in such forms as decimal digits (0 9), letters (A Z and a z), and special symbols (e.g., $, @, %, &, *, (, ), -, +, “, :, ?, / and many others). Digits, letters and special symbols are known as characters. The computer s character set is the set of all characters used to write programs and represent data items. Computers can process only 1s and 0s, so a computer s character set represents every character as a pattern of 1s and 0s. Characters in Java are Unicode characters composed of 2 bytes. Bytes are most commonly composed of eight bits. Programmers create programs and data items with characters. Computers manipulate and process these characters as patterns of bits. See Appendix K for more information on Unicode. Just as characters are composed of bits, fields are composed of characters or bytes. A field is a group of characters or bytes that conveys meaning. For example, a field consisting of uppercase and lowercase letters can be used to represent a person s name. Data items processed by computers form a data hierarchy in which data items become larger and more complex in structure as we progress from bits, to characters, to fields, etc. Typically, several fields (called instance variables in Java) compose a record (implemented as a class in Java). In a payroll system, for example, a record for a particular employee might consist of the following fields (possible data types for these fields are shown in parentheses following each field): Employee identification number (int) Name (String) Address (String) Hourly pay rate (double) Number of exemptions claimed (int) Year-to-date earnings (intor double) Amount of taxes withheld (intor double) Thus, a record is a group of related fields. In the preceding example, each of the fields belongs to the same employee. Of course, a particular company might have many employees and will have a payroll record for each employee. A file is a group of related records.1 A company s payroll file normally contains one record for each employee. Thus, a payroll file for a small company might contain only 22 records, whereas a payroll file for a large company might contain 100,000 records. It is not unusual for a company to have many files, some containing millions, or even billions, of characters of information. Figure 16.1 illustrates the data hierarchy. 1. More generally, a file can contain arbitrary data in arbitrary formats. In some operating systems, a file is viewed as nothing more than a collection of bytes. In such an operating system, any organization of the bytes in a file (such as organizing the data into records) is a view created by the applications programmer.
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Chapter 16 Files and Streams (Cpanel web hosting) 895 Outline 16.1

Thursday, September 27th, 2007

Chapter 16 Files and Streams 895 Outline 16.1 Introduction 16.2 Data Hierarchy 16.3 Files and Streams 16.4 Creating a Sequential-Access File 16.5 Reading Data from a Sequential-Access File 16.6 Updating Sequential-Access Files 16.7 Random-Access Files 16.8 Creating a Random-Access File 16.9 Writing Data Randomly to a Random-Access File 16.10 Reading Data Sequentially from a Random-Access File 16.11 Example: A Transaction-Processing Program 16.12 Class File Summary Terminology Self-Review Exercises Answers to Self-Review Exercises Exercises 16.1 Introduction Storage of data in variables and arrays is temporary the data is lost when a local variable goes out of scope or when the program terminates. Programs use files for long-term retention of large amounts of data, even after programs that create the data terminate. We refer to data maintained in files as persistent data, because the data exists beyond the duration of program execution. Computers store files on secondary storage devices such as magnetic disks, optical disks and magnetic tapes. In this chapter, we explain how Java programs create, update and process data files. We consider both sequential-access files and random-access files and discuss typical applications for each. File processing is one of the most important capabilities a language must have to support commercial applications that typically process massive amounts of persistent data. In this chapter, we discuss Java s powerful file-processing and stream input/output features. File processing is a subset of Java s stream-processing capabilities that enable a program to read and write bytes in memory, in files and over network connections. We have two goals in this chapter to introduce file-processing paradigms and to provide the reader with sufficient stream-processing capabilities to support the networking features introduced in Chapter 17. Software Engineering Observation 16.1 It would be dangerous to enable applets arriving from anywhere on the World Wide Web to be able to read and write files on the client system. By default, Web browsers prevent applets from performing file processing on the client system. Therefore, file-processing programs generally are implemented as Java applications. 16.2 Data Hierarchy Ultimately, a computer processes all data items as combinations of zeros and ones, because it is simple and economical for engineers to build electronic devices that can assume two stable states one state represents 0, the other state represents 1. It is remarkable that the
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

Professional web hosting - 16 Files and Streams Objectives To be

Wednesday, September 26th, 2007

16 Files and Streams Objectives To be able to create, read, write and update files. To understand the Java streams class hierarchy. To be able to use the FileInputStreamand FileOutputStream classes. To be able to use the ObjectInputStreamand ObjectOutputStream classes. To be able to use class RandomAccessFile. To be able to use a JFileChooserdialog to access files and directories. To become familiar with sequential-access and random-access file processing. To be able to use class File. I can only assume that a Do Not File document is filed in a Do Not File file. Senator Frank Church Senate Intelligence Subcommittee Hearing, 1975 Consciousness does not appear to itself chopped up in bits. A river or a stream are the metaphors by which it is most naturally described. William James I read part of it all the way through. Samuel Goldwyn It is quite a three-pipe problem. Sir Arthur Conan Doyle
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.