Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files and Streams The material in this chapter is not tested on the AP CS exams.

Similar presentations


Presentation on theme: "Files and Streams The material in this chapter is not tested on the AP CS exams."— Presentation transcript:

1 Files and Streams The material in this chapter is not tested on the AP CS exams.

2 Objectives: Learn the basic facts about Java’s IO package
Understand the difference between text and binary files Understand the concept of an input or output “stream” Learn a little about exceptions Practice If you want to learn the technical details about Java’s IO package, this is the wrong place. The main objective here is to provide conceptual background on files and streams, with only a glimpse at their implementation in Java. For student projects, EasyReader and EasyWriter are adequate.

3 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. To look at or print out a file you must use some program.

4 Files (cont’d) The file system is 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 system also provides “core” functions, callable from programs, for reading and writing directories and files. Recall that a file is a software entity. The hardware knows nothing about files. The operating system does.

5 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 (e.g., in ASCII code). Each line terminates with a “newline” character (or a combination, carriage return plus line feed). The file name may give a hint as to how it “prefers” to be treated. Surely, a file that contains only ASCII characters and has CR+LF every so often is likely to have been created as an ASCII file and is probably intended to be used that way. But nothing in a file’s contents specifically demands a particular treatment.

6 Text Files Examples: Any plain-text file, typically named something.txt Source code of programs in any language (e.g., Something.java) HTML documents Data files for certain programs, (e.g., fish.dat; any file is a data file for some program.)

7 Binary Files A “binary” file contains any information, any combination of bytes. Only a programmer / designer knows how to interpret it. Different programs may interpret the same file differently (e.g., one program displays an image, another extracts an encrypted message). There have been reports in the press of terrorists using software that hides secret encoded messages in harmless-looking image files. There is also the matter of displaying a file. For example, an HTML file is an ASCII file and may be presented as such in a plain-text editor, but it appears differently in a browser.

8 Binary Files Examples:
Compiled programs (e.g., Something.class) Image files (e.g., something.gif) Music files (e.g., something.mp3) Any file can be treated as a binary file (even a text file, if we forget about the special meaning of CR-LF). The reverse is true, too: you can treat a binary file as a text file. Whenever you encounter a byte that contains a value corresponding to LF, the program will decide that this is the end of a line. In the early releases of MS DOS, files contained a special EOF (end of file) marker at the end. The only problem was that a binary file could have this value in the middle by accident, and the copy command would stop at that place. You needed to add a command switch /b (for binary) to tell the program to treat the file as a binary file.

9 Text as Binary: ASCII display rose.txt A rose is a rose Hex “dump”
The hex dump of a text file in this slide has been produced by the ancient program debug (c. 1982, still included with Windows). The display on the right shows those values that correspond to printable ASCII characters. CR + LF

10 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 actual IO devices, Internet streams, and so on. The advantage of streams is that they provide a uniform way of treating files and devices. For example, in Unix and other operating systems it is easy to “redirect” screen output to a file.

11 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. For text files it is especially important to be able to treat various data sources (a file, an Internet data stream, etc.) uniformly. It is also more common to read or search text files sequentially, from the beginning.

12 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. Thus you can open the same file as a stream in one situation and as a random-access file in another.

13 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 Random-access is convenient. That’s why people have switched from tapes to CDs and from videotapes to DVDs.

14 File Types: Summary File Text Binary Stream Random-Access common use
possible, but not as common

15 java.io How do I read an int from a file?
RandomAccessFile Reader SequenceInputStream SerializablePermission StreamTokenizer StringBufferInputStream StringReader StringWriter Writer BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter DataInputStream DataOutputStream File FileDescriptor FileInputStream FileOutputStream FilePermission FileReader FileWriter FilterInputStream FilterOutputStream FilterReader FilterWriter InputStream InputStreamReader LineNumberInputStream LineNumberReader ObjectInputStream ObjectInputStream.GetField ObjectOutputStream ObjectOutputStream.PutField ObjectStreamClass ObjectStreamField OutputStream OutputStreamWriter PipedInputStream PipedOutputStream PipedReader PipedWriter PrintStream PrintWriter PushbackInputStream PushbackReader How do I read an int from a file? Java IO is not for the faint at heart. It looks like the developers went all out to confuse programmers. That’s why programmers are such well-paid specialists.


Download ppt "Files and Streams The material in this chapter is not tested on the AP CS exams."

Similar presentations


Ads by Google