Java Methods ObjectOriented Programming and Data Structures 3
Java Methods Object-Oriented Programming and Data Structures 3 rd AP edition Maria Litvin ● Gary Litvin output. File. printf ("Chapter %d", 15); Streams and Files Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. 1
Objectives: • Learn the basic facts about the Java IO package • Understand the difference between text and binary files • Understand the concept of an input or output stream • Learn about handling exceptions 2
Files • A file is a collection of data in mass storage. • A data file is not a part of a program’s source code. • The same file can be read or modified by different programs. • The program must be aware of the format of the data in the file. 3
Files (cont’d) • The files are maintained by the operating system. • The system provides commands and/or GUI utilities for viewing file directories and for copying, moving, renaming, and deleting files. • The operating system also provides basic functions, callable from programs, for reading and writing directories and files. 4
Text Files • A computer user distinguishes text (“ASCII”) files and “binary” files. This distinction is based on how you treat the file. • A text file is assumed to contain lines of text (for example, in ASCII code). • Each line terminates with a newline character (or a combination, carriage return plus line feed). 5
Text Files • Examples: Ø Ø Any plain-text file, typically named something. txt Source code of programs in any language (for example, Something. java) HTML documents Data files for certain programs, (for example, fish. dat; any file is a data file for some program. ) 6
Binary Files • A “binary” file can contain any information, any combination of bytes. • Only a programmer / designer knows how to interpret it. • Different programs may interpret the same file differently (for example, one program displays an image, another extracts an encrypted message). 7
Binary Files • Examples: Ø Ø Ø Compiled programs (for example, Something. class) Image files (for example, something. gif) Music files (for example, something. mp 3) • Any file can be treated as a binary file (even a text file, if we forget about the special meaning of CR-LF). 8
Text as Binary: rose. txt A rose is a rose ASCII display Hex “dump” CR + LF 9
Streams • A stream is an abstraction derived from sequential input or output devices. • An input stream produces a stream of characters; an output stream receives a stream of characters, “one at a time. ” • Streams apply not just to files, but also to IO devices, Internet streams, and so on. 10
Streams (cont’d) • A file can be treated as an input or output stream. • In reality file streams are buffered for efficiency: it is not practical to read or write one character at a time from or to mass storage. • It is common to treat text files as streams. 11
Random-Access Files • A program can start reading or writing a random-access file at any place and read or write any number of bytes at a time. • “Random-access file” is an abstraction: any file can be treated as a random-access file. • You can open a random-access file both for reading and writing at the same time. 12
Random-Access Files (cont’d) • A binary file containing fixed-length data records is suitable for random-access treatment. • A random-access file may be accompanied by an “index” (either in the same or a different file), which tells the address of each record. • Tape : CD == Stream : Random-access 13
File Types: Summary File Text Binary Stream Random-Access common use possible, but not as common 14
java. io Buffered. Input. Stream Buffered. Output. Stream Buffered. Reader Buffered. Writer Byte. Array. Input. Stream Byte. Array. Output. Stream Char. Array. Reader Char. Array. Writer Data. Input. Stream Data. Output. Stream File. Descriptor File. Input. Stream File. Output. Stream File. Permission File. Reader File. Writer Filter. Input. Stream Filter. Output. Stream Filter. Reader Filter. Writer Input. Stream. Reader Line. Number. Input. Stream Line. Number. Reader Object. Input. Stream. Get. Field Object. Output. Stream. Put. Field Object. Stream. Class Object. Stream. Field Output. Stream. Writer Piped. Input. Stream Piped. Output. Stream Piped. Reader Piped. Writer Print. Stream Print. Writer Pushback. Input. Stream Pushback. Reader Random. Access. File Reader Sequence. Input. Stream Serializable. Permission Stream. Tokenizer String. Buffer. Input. Stream String. Reader String. Writer How do I read an int from a file? 15
java. io (cont’d) • Uses four hierarchies of classes rooted at Reader, Writer, Input. Stream, Output. Stream. • Input. Stream/Output. Stream hierarchies deal with bytes. Reader/Writer hierarchies deal with chars. • Has a special stand-alone class Random. Access. File. • The Scanner class has been added to java. util in Java 5 to facilitate reading numbers and words. 16
java. io. File • The File class represents a file (or folder) in the file directory system. String pathname = ". . /Data/words. txt“; File file = new File(pathname); • Methods: String get. Name( ) String get. Absolute. Path( ) long length( ) boolean is. Directory( ) File[ ] list. Files( ) 17
Reading from a Text File String pathname = "words. txt"; Tries to open File file = new File(pathname); the file Scanner input = null; try { input = new Scanner(file); } catch (File. Not. Found. Exception ex) { System. out. println("*** Cannot open " + pathname + " ***"); System. exit(1); // quit the program } 18
Scanner Methods boolean has. Next. Line() String next. Line() boolean has. Next() String next() boolean has. Next. Int() int next. Int() boolean has. Next. Double() double next. Double() void close() Reads one “word” 19
Writing to a Text File String pathname = "output. txt"; File file = new File(pathname); Print. Writer output = null; try { output = new Print. Writer(file); } catch (File. Not. Found. Exception ex) { System. out. println("Cannot create " + pathname); System. exit(1); // quit the program } output. println(. . . ); output. printf(. . . ); Required to flush output. close(); the output buffer 20
Review: • Name a few types of files that are normally treated as text files. • Can you open the same file as a text file and as a binary file in different programs? • Can you open a text file as a random-access file? • Do you think. jar (Java archives) files that contain compiled library classes are treated as streams or random-access files? 21
- Slides: 21