Fish say, they have their stream and pond, But is there anything beyond?
—RUPERT BROOKE, HEAVEN
*OUIJTTFDUJPOXFHJWFZPVBHFOFSBMJOUSPEVDUJPOUPGJMF*0*OQBSUJDVMBSXF FYQMBJOUIFEJGGFSFODFCFUXFFOUFYUGJMFTBOECJOBSZGJMFT5IF+BWBTZOUBYGPS GJMF*0TUBUFNFOUTJTHJWFOJOTVCTFRVFOUTFDUJPOTPGUIJTDIBQUFS
The Concept of a Stream
:PV BSF BMSFBEZ VTJOH GJMFT UP TUPSF ZPVS +BWB DMBTTFT BOE QSPHSBNT ZPVS NVTJD ZPVS QJDUVSFT BOE ZPVS WJEFPT :PV DBO BMTP VTF GJMFT UP TUPSF JOQVU GPSBQSPHSBNPSUPIPMEPVUQVUGSPNBQSPHSBN*O+BWBGJMF*0BTXFMMBT simple keyboard and screen I/O, is handled by streams. A stream is a flow of data. The data might be characters, numbers, or bytes consisting of binary digits. If the data flows into your program, the stream is called an input stream.
If the data flows out of your program, the stream is called an output stream.
For example, if an input stream is connected to the keyboard, the data flows from the keyboard into your program. If an input stream is connected to a file, the data flows from the file into your program. Figure 10.1 illustrates some of these streams.
*O +BWB TUSFBNT BSF JNQMFNFOUFE BT PCKFDUT PG TQFDJBM TUSFBN DMBTTFT Objects of the class ScannerXIJDIXFIBWFCFFOVTJOHGPSLFZCPBSEJOQVU are input streams. The object System.out is an example of an output stream
Files can store programs, music, pictures, video, and so on
A stream is a flow of data into or out of a program
FIGURE 10.1 Input and Output Streams
Input stream
Output stream
Input stream Keyboard
Compact disc
Output stream
Monitor
Hard disk Program
728 CHAPTER 10 / Streams and File I/O
UIBUXFIBWFBMTPVTFE*OUIJTDIBQUFSXFEJTDVTTTUSFBNTUIBUDPOOFDUZPVS program to files instead of to the keyboard or display.
RECAP Streams
A stream is an object that either
t %FMJWFSTEBUBGSPNZPVSQSPHSBNUPBEFTUJOBUJPOTVDIBTBGJMFPSUIF screen, or
t 5BLFTEBUBGSPNBTPVSDFTVDIBTBGJMFPSUIFLFZCPBSEBOEEFMJWFST UIFEBUBUPZPVSQSPHSBN
Why Use Files for I/O?
5IFLFZCPBSEJOQVUBOETDSFFOPVUQVUXFIBWFVTFETPGBSEFBMXJUIUFNQPSBSZ data. When the program ends, the data typed at the keyboard and left on the TDSFFOHPBXBZ'JMFTQSPWJEFZPVXJUIBXBZUPTUPSFEBUBQFSNBOFOUMZ5IF contents of a file remain until a person or program changes the file.
"OJOQVUGJMFDBOCFVTFEPWFSBOEPWFSBHBJOCZEJGGFSFOUQSPHSBNTXJUIPVU UIFOFFEUPUZQFUIFEBUBBHBJOGPSFBDIQSPHSBN'JMFTBMTPQSPWJEFZPVXJUIB DPOWFOJFOUXBZUPEFBMXJUIMBSHFRVBOUJUJFTPGEBUB8IFOZPVSQSPHSBNUBLFT JUTJOQVUGSPNBMBSHFJOQVUGJMFJUSFDFJWFTBMPUPGEBUBXJUIPVUVTFSFGGPSU
Text Files and Binary Files
All of the data in any file is stored as binary digits (bits)—that is, as a long TFRVFODF PG T BOE T )PXFWFS JO TPNF TJUVBUJPOT XF EP OPU UIJOL PG B file’s contents as a sequence of binary digits. Instead, we think of them as a sequence of characters. Files that are thought of as sequences of characters, BOE UIBU IBWF TUSFBNT BOE NFUIPET UP NBLF UIF CJOBSZ EJHJUT MPPL MJLF characters to your program and your text editor, are called text files. All other files are called binary files.&BDILJOEPGGJMFIBTJUTPXOTUSFBNTBOENFUIPET to process them.
:PVS +BWB QSPHSBNT BSF TUPSFE JO UFYU GJMFT :PVS NVTJD GJMFT BOE QJDUVSF files are binary files. Since text files are sequences of characters, they usually BQQFBSUIFTBNFPOBMMDPNQVUFSTTPZPVDBONPWFZPVSUFYUGJMFTGSPNPOF computer to another with few or no problems. The contents of binary files are often based on numbers. The structure of some binary files is standardized so UIBU UIFZ DBO BMTP CF VTFE PO B WBSJFUZ PG QMBUGPSNT .BOZ QJDUVSF GJMFT BOE music files fall into this category.
+BWBQSPHSBNTDBODSFBUFPSSFBECPUIUFYUGJMFTBOECJOBSZGJMFT8SJUJOHB text file and writing a binary file require similar steps. Likewise, reading a text GJMF JT TJNJMBS UP SFBEJOH B CJOBSZ GJMF 5IF LJOE PG GJMF IPXFWFS EFUFSNJOFT which classes we use to perform the input and output.
%BUBJOBGJMF remains after program execution ends
5XPLJOETPGGJMFT text files and CJOBSZGJMFT
5IF POF CJH BEWBOUBHF PG UFYU GJMFT JT UIBU ZPV DBO DSFBUF MPPL BU BOE FEJUUIFNCZVTJOHBUFYUFEJUPS:PVIBWFBMSFBEZEPOFUIJTXIFOXSJUJOHB +BWBQSPHSBN8JUICJOBSZGJMFTBMMUIFSFBEJOHBOEXSJUJOHNVTUOPSNBMMZCF done by a special program. Some binary files must be read by the same type of computer and with the same programming language that originally created UIFN)PXFWFS+BWBCJOBSZGJMFTBSFQMBUGPSNJOEFQFOEFOUUIBUJTXJUI+BWB ZPV DBO NPWF ZPVS CJOBSZ GJMFT GSPN POF UZQF PG DPNQVUFS UP BOPUIFS BOE ZPVS+BWBQSPHSBNTXJMMTUJMMCFBCMFUPSFBEUIFCJOBSZGJMFT
&BDIDIBSBDUFSJOBUFYUGJMFJTSFQSFTFOUFEBTPSCZUFTEFQFOEJOHPO XIFUIFSUIFTZTUFNVTFT"4$**PS6OJDPEF8IFOBQSPHSBNXSJUFTBWBMVFUP a text file, the number of characters written is the same as if they were written to a display using System.out.println. For example, writing the int WBMVF UPBUFYUGJMFQMBDFTGJWFDIBSBDUFSTJOUIFGJMFBTTIPXOJO'JHVSF In general, writing an integer places between 1 and 11 characters in a text file.
#JOBSZGJMFTTUPSFBMMWBMVFTPGUIFTBNFQSJNJUJWFEBUBJOUIFTBNFGPSNBU
&BDI JT TUPSFE BT B TFRVFODF PG UIF TBNF OVNCFS PG CZUFT 'PS FYBNQMF BMM intWBMVFTPDDVQZCZUFTFBDIJOBCJOBSZGJMFBT'JHVSFBMTPJMMVTUSBUFT
" +BWB QSPHSBN JOUFSQSFUT UIFTF CZUFT JO WFSZ NVDI UIF TBNF XBZ UIBU JU interprets a data item, such as an integer, in the computer’s main memory.
That is why binary files can be handled so efficiently.
FIGURE 10.2A Text File and a Binary File Containing the Same Values
1 2 3 4 5 - 4 0 2 7 8 . . .
12345 -4072 8 . . . A text file
A binary file
REMEMBER Input and Output Terminology
5IFXPSEinputNFBOTUIBUEBUBNPWFTJOUPZPVSQSPHSBNOPUJOUPUIF GJMF5IFXPSEoutputNFBOTUIBUEBUBNPWFTPVUPGZPVSQSPHSBNOPU out of the file.
FAQ Should I use a text file or a binary file?
6TFBUFYUGJMFJGZPVXBOUBUFYUFEJUPSUPFJUIFSDSFBUFBGJMFUIBUB program will read or read a file that a program created. In other cases, DPOTJEFSBCJOBSZGJMFBTJUVTVBMMZPDDVQJFTMFTTTQBDF
730 CHAPTER 10 / Streams and File I/O
S E L F - T E S T Q U E S T I O N S
1. Why would anybody write a program that sends its output to a file instead of to the screen?
8IFOXFEJTDVTTJOQVUBSFXFSFGFSSJOHUPEBUBNPWJOHGSPNUIFQSPHSBN to a file or from a file to the program?
3. What is the difference between a text file and a binary file?