Download Java I/O: Files and Streams and more Schemes and Mind Maps Java Programming in PDF only on Docsity!
LECTURE 09: WORKING WITH
FILES
o File & Stream
o Binary Files
o Text Files
o Serialization
PLAN
o Java views each file as a sequential stream of bytes
o Every operating system provides a mechanism to determine the
end of a file, such as an end-of-file marker or a count of the total
bytes in the file that is recorded in a system-maintained
administrative data structure.
o A Java program simply receives an indication from the operating
system when it reaches the end of the stream
Files and Streams
o File streams can be used to input and output data as bytes or
characters.
o Streams that input and output bytes are known as byte-based streams,
representing data in its binary format.
o Streams that input and output characters are known as character-based
streams, representing data as a sequence of characters.
o Files that are created using byte-based streams are referred to as
binary files.
o Files created using character-based streams are referred to as text
files. Text files can be read by text editors.
o Binary files are read by programs that understand the specific content
of the file and the ordering of that content.
Files and Streams (cont.)
o Java programs perform file processing by using classes from
package java.io.
o Includes definitions for stream classes
- FileInputStream (for byte-based input from a file)
- FileOutputStream (for byte-based output to a file)
- FileReader (for character-based input from a file)
- FileWriter (for character-based output to a file)
o You open a file by creating an object of one these stream classes.
The object’s constructor opens the file.
Files and Streams (cont.)
o Can perform input and output of objects or variables of primitive data
types without having to worry about the details of converting such
values to byte format.
o To perform such input and output, objects of classes ObjectInputStream
and ObjectOutputStream can be used together with the byte-based file
stream classes FileInputStream and FileOutputStream.
o The complete hierarchy of classes in package java.io can be viewed in
the online documentation at
o http://download.oracle.com/javase/6/docs/api/java/io/package-tree.html
Files and Streams (cont.)
o Class File provides four constructors.
o The one with a String argument specifies the name of a file or directory
to associate with the File object.
- The name can contain path information as well as a file or directory
name.
- A file or directory’s path specifies its location on disk.
- An absolute path contains all the directories, starting with the root
directory, that lead to a specific file or directory.
- A relative path normally starts from the directory in which the
application began executing and is therefore “relative” to the current
directory.
Class File
o The constructor with two String arguments specifies an absolute or
relative path and the file or directory to associate with the File object.
o The constructor with File and String arguments uses an existing File
object that specifies the parent directory of the file or directory specified
by the String argument.
o The fourth constructor uses a URI object to locate the file.
- A Uniform Resource Identifier (URI) is a more general form of the
Uniform Resource Locators (URLs) that are used to locate websites.
o Figure 17.2 lists some common File methods. The
o http://download.oracle.com/javase/6/docs/api/java/io/File.html
Class File (cont.)
o FileOutputStream and FileInputStream are two stream objects that
facilitate file access.
o FileOutputStream allows us to output a sequence of bytes; values
of data type byte.
o FileInputStream allows us to read in an array of bytes.
Streams for Low-Level File I/O
Sample: Low-Level File
Output
//set up file and stream File outFile = new File("sample1.data"); FileOutputStream outStream = new FileOutputStream( outFile ); //data to save byte[] byteArray = {10, 20, 30, 40, 50, 60, 70, 80}; //write data to the stream outStream.write( byteArray ); //output done, so close the stream outStream.close();
o FileOutputStream and DataOutputStream are used to output
primitive data values
o FileInputStream and DataInputStream are used to input primitive
data values
o To read the data back correctly, we must know the order of the
data stored and their data types
Streams for High-Level File I/O
- A standard sequence to set up a DataOutputStream object:
Setting up DataOutputStream
File outFile = new File( "sample2.data" ); FileOutputStream outFileStream = new FileOutputStream(outFile); DataOutputStream outDataStream = new DataOutputSteam(outFileStream);
- A standard sequence to set up a DataInputStream object:
Setting up DataInputStream
File inFile = new File( "sample2.data" ); FileInputStream inFileStream = new FileInputStream(inFile); DataInputStream inDataStream = new DataInputSteam(inFileStream);
Sample Input
import java.io.*; class Ch12TestDataInputStream { public static void main (String[] args) throws IOException {
... //set up inDataStream //read values back from the stream and display them System.out.println(inDataStream.readInt()); System.out.println(inDataStream.readLong()); System.out.println(inDataStream.readFloat()); System.out.println(inDataStream.readDouble()); System.out.println(inDataStream.readChar()); System.out.println(inDataStream.readBoolean()); //input done, so close the stream inDataStream.close(); } }