Java - I/O Classes - Florida State University

[Pages:20]I/O CLASSES

Summer 2019

FILE CLASS

? File is a class used for retrieving properties of files or directories on a disk ? Objects of class File do not open files or provide file processing features ? There are a few constructors, including one that just takes a String representing the path to the file/directory

? Many of the methods in the File class are purely for gathering information. Examples: ? canExecute() ? Does this application have execute permissions on the file? ? getAbsolutePath() ? Returns the absolute pathname of the file as a String ? isFile() ? Is this a normal file? ? isDirectory() ? Is this a directory? ? exists() ? Does this file/directory even exist? ? list() ? Returns an array of Strings with the names of files contained in this directory ? listFiles() - Returns an array of Files with the names of files contained in this directory

FILE CLASS

? Other methods actually make changes that don't require the file to be opened. Examples: ? delete() ? deletes this file or directory ? deleteOnExit() ? deletes this file or directory when the JVM exits ? mkdir() ? creates a directory with the path of this File object ? setReadOnly() ? sets permissions of this file/directory to read-only

STREAMS

? In Java, I/O is handled with streams ? A stream is a buffer of data flowing from one place to another

? An input stream flows from an input source (keyboard, file, etc.) into a program, usually to be stored in variables

? An output stream flows from a program to an output destination (screen, file, etc.)

BUILT-IN STREAMS

? Three stream objects are already associated with devices in Java ? System.in ? standard input stream object ? System.out ? standard output stream object ? System.err ? standard error stream object

? Many I/O libraries are in the package java.io ? You can see the hierarchy of java.io classes here

BUILT-IN STREAMS

? There are many built-in stream classes in Java for processing different types of data. Two primary categories: byte streams and character streams ? InputStream is the base class for byte stream input classes ? OutputStream is the base class for byte stream output classes ? Reader is the base class for character stream input classes ? Writer is the base class for character stream output classes

? Byte streams read one byte at a time (binary data) ? Character streams read one character at a time (unformatted text)

STREAMS

? When using streams, note that there are often checked exceptions that need to be handled. So pay attention to which methods can throw them. ? Example: Input streams will have various read() methods that may throw an

IOException

? Also be aware that the various streams should be closed when you finish with them ? This is done using the close() method ? Streams are all considered resources, so you can use try with resources to close them, as well (review the exception slides if you need to)

TYPES OF FILES

? Text File: A file made of readable text characters, looks like screen output ? Binary File: A binary file containing unformatted data saved in its raw memory format ? Text files are typically created with character-based streams, while binary files are

typically created with byte streams ? Sequential File: No regular record structure, typically read or written as an entire file ? Random Access File: Structured as uniformly-sized records. Can read/write either

sequentially or by accessing records anywhere in the file

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download