File and Stream
File and Stream
Introduction
• Storage of data in variables and arrays is temporary—
the data is lost when a local variable goes out of scope
or when the program terminates. Computers use files
for long-term retention of large amounts of data, even
after the programs that created the data terminate.
• We refer to data maintained in files as persistent data
because it exists beyond the duration of program
execution. Computers store files on secondary storage
devices such as hard disks, optical disks and magnetic
tapes.
Files and Streams
• The term “stream” refers to ordered data that
is read from or written to a file.
• Java views each file as a sequential stream of
bytes. 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.
• File streams can be used to input and output
data as either characters or bytes. Streams
that input and output bytes to files are known
as byte-based streams, storing data in its
binary format. Streams that input and output
characters to files are known as character
based streams, storing data as a sequence of
characters.
• For instance, if the value 5 were being stored
using a byte-based stream, it would be stored
in the binary format of the numeric value 5, or
101. If the value 5 were being stored using a
character-based stream, it would be stored in
the binary format of the character 5, or
00000000 00110101 (this is the binary for the
numeric value 53, which indicates the
character 5 in the Unicode character set).
• Files that are created using byte-based streams
are referred to as binary files, while files created
using character-based streams are referred to as
text files. Text files can be read by text editors,
while binary files are read by a program that
converts the data to a human-readable format.
• A Java program opens a file by creating an
object and associating a stream of bytes or
characters with it.
• Java programs perform file processing by using
classes from package java.io. This package includes
definitions for stream classes, such as
FileInputStream (for byte-based input from a file),
FileOutputStream (for byte-based output to a file),
FileReader (for character-based input from a file) and
FileWriter (for character-based output to a file).
• Files are opened by creating objects of these stream
classes, which inherit from classes InputStream,
OutputStream, Reader and Writer, respectively.
File and FileDialog Objects
• The class “File” is used for retrieving information about
files or directories from disk. Objects of class File do not
open files or provide any file-processing capabilities.
• However, File objects are used frequently with objects of
other java.io classes to specify files or directories to
manipulate.
• Class File provides four constructors.
• public File( String name )
• public File( String pathToName, String name )
• public File( File directory, String name )
• public File( URI uri )
• Suppose we want to read the contents of a file sample.dat.
before we begin the actual operation of reading data from
this file, we must first create a File object and associate it
to the file. We do so by calling a File constructor:
File inFlie = new File(“sample.dat”);
• The system assumes the file is located in the current
directory. It is also possible to open a file that is stored in a
directory other than the current directory by providing a
path name and a file name. assuming there’s a file xyz.dat
in the projects directory, we can open it by executing:
File inFile = new File(“c:\\cafe\\projects”, “xyz.dat”);
• FileDialog: we can use a FileDialog object from
the package java.awt to let the user select a
file or a directory. The object has two modes:
LOAD and SAVE. You open the object in the
LOAD mode if you want to read data from the
selected file and in the SAVE mode if you want
to write data to the selected file.
I/O Classes
• the most commonly used I/O classes are
summarized below:
• Object
– File
– FileDescriptor
– InputStream
– OutputStream
– RandomAccessFile
– Reader
– Writer
• The InputStream and Reader classes are for input, and
the OutputStream and Writer classes are for output.
The InputStream and OutputStream classes are for
processing binary files and the Reader and writer
classes are for processing text files.
• Java makes a clear distinction between binary files and
text files. A binary file is one that is read and written
by executing programs. Image files, audio files, and
executable programs themselves are binary files. A
text file is one that is read and written by text editors.
Low-Level File I/O
• In order to actually read data from or write data to a file,
we must create one of the Java stream objects and attach
it to the file. A stream is simply a sequence of data items,
usually eight-bit byte. Java has two types of streams: an
input stream and output stream. An input stream has a
source from where the data items come(used for reading),
and an output stream has a destination to where the data
items are going(used for writing).
• Notice that we are not dealing with the File object dircely,
but with the outStream object.
• import java.io.*;
• import javax.swing.*;
• public class TestFileOutputStream {
• private static String fileName;
• private static FileOutputStream fOS;
• private static File outPutFile;
• private static byte byteArray[]={10,20,30,40,50};
• public static void main(String[] args) {
• fileName = JOptionPane.showInputDialog(null,"Enter the file name:");
• try{
• outPutFile = new File(fileName);
• fOS = new FileOutputStream(outPutFile);
• fOS.write(byteArray);
• System.out.println("File has been saved");
• } catch(IOException ioe){
• ioe.printStackTrace();
• }
• finally{
• try{
• fOS.close();
• }catch(IOException ioe){
• ioe.printStackTrace();
• }
• }
• }
• }
• To read the data into a program, we reverse
the steps in the output routine. We use the
read method of FileInputStream to read in an
array of bytes.
• import java.io.*;
• import javax.swing.*;
• public class TestFileInputStream {
• public static void main(String[] args)throws IOException {
• File inPutFile = new File("c:\\fileTest");
• FileInputStream fis = new FileInputStream(inPutFile);
• String output="";
• byte[] byteArray = new byte[(int)inPutFile.length()]; //set up an array to read data in
• fis.read(byteArray);//read data in and display them
• for(int i=0;i<byteArray.length;i++)
• output += "\n"+byteArray[i];
• fis.close();//input done, so close the stream.
• System.out.println("Finished reading");
• JOptionPane.showMessageDialog(null,"The content is : "+output,"Reading from file",
JOptionPane.INFORMATION_MESSAGE);
• }
• }
High-Level File I/O:
• By using DataOutputStream, we can output java primitive
data type values. A DataOutputStream object will take
care of the details of converting the primitive data type
values to a sequence of bytes.
• The argument to the DataOutputStream constructor is a
FileOutputStream object. A DataOutputStream object
does not get connected to a file directly. The role of the
DataOutputStream object is to provide high-level access
to a file by converting a primitive data value into a
sequence of bytes, which are written to a file via
FileOutputStream object.
• import java.io.*;
• import javax.swing.*;
• public class TestDataOutputStream {
• public static void main(String[] args) throws IOException {
• File outFile = new File("C:\\fileTest2.dat");
• FileOutputStream outFileStream = new FileOutputStream(outFile);
• DataOutputStream outDataStream = new DataOutputStream(outFileStream);
• //write values of primitive data types to the stream
• outDataStream.writeInt(9875);
• outDataStream.close();//output done, so close the stream
• System.out.println("Finished writing to file");
• String output="\n";
• //set up file and streams
• File inFile = new File("C:\\fileTest2.dat");
• FileInputStream inFileStream = new FileInputStream(inFile);
• DataInputStream inDataStream = new DataInputStream(inFileStream);
• output += "The content of "+inFile.getName()+" is \n";
• //read values back from the stream and display them
• output += inDataStream.readInt()+"\n";
• inDataStream.close();//input done, so close the stream
• JOptionPane.showMessageDialog(null,output);//now display the content of the file
• }
• }
• Both FileOutputStream andDataOutputStream objects
produce a binary file in which the contents are stored
in the format (called binary format).
• Instead of storing data in binary format, we can store
them in ASCII format. With the ASCII format, all the
data are converted to String data. A file whose content
are stored in ASCII format is called a text file.
• One majored benefit of a text file is that, we can easily
read and modify the contents of a text file using any
text editor or word processor.
import java.io.*;
public class TestText {
public static void main(String args[])throws IOException
{
File outFile = new File("a:\\test1.dat");
FileWriter outFileWriter = new FileWriter(outFile);
PrintWriter outPrintWriter = new PrintWriter(outFileWriter);
//now write to the file
outPrintWriter.write("this is a sample text");
outPrintWriter.close();
System.out.println("text written");
}
}//end class
• PrintWriter is an object we use to generate a
text file. Unlike DataOutputStream, where we
have a separate write method for each
individual data type, it provides overloaded
methods: print, println and write.
• To read the data from a text file using a code,
we use the FileReader and BufferedReader
objects. Consider the following code:
import java.io.*;
public class TestTextReader {
public static void main(String args[]) throws IOException
{
File inFile = new File("a:\\test1.txt");
FileReader inFileReader = new FileReader(inFile);
BufferedReader inBufReader = new BufferedReader(inFileReader);
String str;
while((str=inBufReader.readLine())!=null)
{
System.out.println(str);
}
inBufReader.close();
}
}//end class
Object I/O
• When an instance variables are output to a disk file,
certain information will be lost, such as the type of
each value. For instance, if the value "3" were read
from a file, there is no way to tell whether the value
came from an int, a String or a double. We have
only data, not type information, on a disk.
• Sometimes we will not know exactly how the data is
stored in a file. In such cases, we would like to read
or write an entire object from a file. Java provides
such a mechanism, called object serialization.
• Serialization is the process of writing the state of an object
to a byte stream. This is useful when you want to save the
state of your program to a persistent storage area, such as
a file. At a later time, you may restore these objects by
using the process of deserialization.
• Serialization is also needed to implement Remote Method
Invocation (RMI). RMI allows a Java object on one machine
to invoke a method of a Java object on a different machine.
An object may be supplied as an argument to that remote
method. The sending machine serializes the object and
transmits it. The receiving machine deserializes it.
• Only an object that implements the
Serializable interface can be saved and
restored by the serialization facilities. The
Serializable interface defines no members. It is
simply used to indicate that a class may be
serialized. If a class is serializable, all of its
subclasses are also serializable.
• Variables that are declared as static are not
saved by the serialization facilities.
• Classes ObjectInputStream and ObjectOutputStream,
which respectively implement the ObjectInput and
ObjectOutput interfaces, enable entire objects to be read
from or written to a stream (possibly a file).
• To use serialization with files, we initialize Object-
InputStream and ObjectOutputStream objects with stream
objects that read from and write to files—objects of
classes FileInputStream and FileOutputStream,
respectively.
• Initializing stream objects with other stream objects in this
manner is sometimes called wrapping.
• The ObjectOutput interface contains method
writeObject, which takes an Object that
implements interface Serializable (discussed
shortly) as an argument and writes its
information to an OutputStream.
• Correspondingly, the ObjectInput interface
contains method readObject, which reads and
returns a reference to an Object from an Input
Stream.
RandomAccessFile
• RandomAccessFile encapsulates a random-
access file.
• it implements the interfaces DataInput and
DataOutput, which define the basic I/O
methods. It also supports positioning requests
—that is, you can position the file pointer
within the file. It has these two constructors:
• RandomAccessFile(File fileObj, String access)throws
FileNotFoundException
• RandomAccessFile(String filename, String access)
throws FileNotFoundException
• In the first form, fileObj specifies the name of the file
to open as a File object. In the second form, the name
of the file is passed in filename. In both cases, access
determines what type of file access is permitted. If it is
“r”, then the file can be read, but not written. If it is
“rw”, then the file is opened in read-write mode.
• The method seek( ) is used to set the current position
of the file pointer within the file:
• void seek(long newPos) throws IOException
• Here, newPos specifies the new position, in bytes, of
the file pointer from the beginning of the file. After a
call to seek( ), the next read or write operation will
occur at the new file position.
• RandomAccessFile implements the standard input and
output methods, which you can use to read and write
to random access files.