2CS1010601 AdvJava - UNIT 2 Files
2CS1010601 AdvJava - UNIT 2 Files
Concept of Streams
✓ A stream is a path along which the data flows.
✓ Every stream has a source and a destination.
✓ We can build a complex file processing sequence using a series of simple stream
operations.
✓ A stream is a sequence of data.
✓ In Java, a stream is composed of bytes.
✓ Input refers to the flow of data into a program and output means the flow of data
out of a program.
✓ Input to a program may come from keyboard, mouse, memory, disk, network or
another program.
[ Page 1 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
✓ Output from program may go to the screen, printer, memory, disk, network or
another program.
Input Output
[ Page 2 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
Stream Classes
✓ The java.io package contains a large number of stream classes that provide
capabilities for processing all types of data.
✓ These classes may be categorized into two groups based on the data type on
which they operate.
[ Page 3 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
Creation of Files
✓ If we want to create and use a file, we need to decide the following about the file
and its intended purpose:
• Suitable name for the file.
• Data type to be stored.
• Purpose (reading, writing or updating).
• Method of creating the file.
✓ A file name is a unique string of characters that helps identify a file on the disk.
✓ The length of a file name and the characters allowed are dependent on the OS on
which the Java program is executed.
✓ A file name may contain two parts, a primary name and an optional period with
extension.
[ Page 4 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
✓ Example:
• input.data, salary, test.doc, student.txt, inventory, employee
✓ Data type is important to decide the type of file stream classes to be used for
handling the data like characters, bytes or primitive type.
✓ The purpose of using a file must also be decided before using it for reading only
or writing only or both the operations.
✓ A file stream can be defined using the classes of Reader/InputStream for
reading data and Writer/OutputStream for writing data.
✓ There are two ways of initializing the file stream objects.
✓ All of the constructors require that we provide the name of the file either
directly (as a literal string or variable) or indirectly by giving a file object that
has already been assigned a filename.
1. Direct approach
Example :
FileInputStream fis;
try
{ //assign the filename to the file stream object
fis = new FileInputStream(“test.dat”);
……………..
}
catch(IOException e)
{ …………….. }
2. Indirect approach
• The indirect approach uses a file object that has been initialized with the
desired filename.
Example :
File fname;
fname = new File(“test.dat”);
FileInputStream fis;
try
{ //give the value of the file object to the file stream object
fis = new FileInputStream(fname);
……………..
}
catch(IOException e)
{ …………….. }
[ Page 5 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
InputStream Classes
• Input stream classes are used to read 8-bit bytes from the stream.
• The InputStream class is the super class for all byte-oriented input stream
classes.
• All the methods of this class throw an IOException.
• The InputStream class is an abstract class, and therefore, we cannot create
instances of this class.
• We must use the subclasses that inherit from this class.
InputStream Classes
1. BufferedInputStream
▪ Contains methods to read bytes from the buffer (memory area).
2. ByteArrayInputStream
▪ Contains methods to read bytes from a byte array.
3. DataInputStream
▪ Contains methods to read Java primitive data types.
4. FileInputStream
▪ Contains methods to read bytes from a file.
5. FilterInputStream
▪ Contains methods to read bytes from other input streams which it uses as its
basic source of data.
6. ObjectInputStream
▪ Contains methods to read objects.
7. PipedInputStream
▪ Contains methods to read from a piped output stream. A piped input stream
must be connected to a piped output stream.
8. SequenceInputStream
▪ Contains methods to concatenate multiple input streams and then read from
the combined stream.
[ Page 7 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
8. void close ()
▪ Closes the input stream.
• Note : the class DataInputStream extends FilterInputStream and
implements the interface DataInput. Therefore, the DataInputStream class
implements the methods described in DataInput in addition to using the
methods of InputStream class.
• The DataInput interface contains the following methods:
• readshort() • readDouble()
• readInt() • readLine()
• readLong() • readChar()
• readFloat() • readBoolean()
[ Page 8 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
OutputStream Classes
• Java's output stream classes are used to write 8-bit bytes to a stream.
• The OutputStream class is the super class for all byte-oriented output stream
classes.
• OutputStream class is an abstract class.
• An output stream accepts output bytes and sends them to some link.
• All the methods of this class throw an IOException.
• Being an abstract class, the OutputStream class cannot be instantiated.
• The several subclasses of the OutputStream can be used for performing the
output operations.
OutputStream Classes
1. BufferedOutputStream
▪ Contains methods to write bytes into the buffer.
[ Page 9 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
2. ByteArrayOutputStream
▪ Contains methods to write bytes into a byte array.
3. DataOutputStream
▪ Contains methods to write Java primitive data types.
4. FileOutputStream
▪ Contains methods to write bytes to a file.
5. FilterOutputStream
▪ Contains methods to write to other output streams.
6. ObjectOutputStream
▪ Contains methods to write objects.
7. PipedOutputStream
▪ Contains methods to write to a piped output stream.
8. PrintStream
▪ Contains methods to print Java primitive data types.
• The OutputStream class defines methods to perform writing operations.
5. void close ()
▪ Closes the output stream.
• Note: the class DataOutputStream extends FilterOutputStream and
implements the interface DataOutput. Therefore, the DataOutputStream
class implements the methods described in DataOutput in addition to using
the methods of OutputStream class.
• The DataOutput interface contains the following methods:
• writeshort() • writeDouble()
• writeInt() • writeLine()
• writeLong() • writeChar()
• writeFloat() • writeBoolean()
[ Page 12 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
[ Page 13 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
Reader Classes
1. BufferedReader
▪ Contains methods to read characters from the buffer.
2. CharArrayReader
▪ Contains methods to read characters from a character array.
3. FileReader
▪ Contains methods to read from a file.
4. FilterReader
▪ Contains methods to read from underlying character-input stream.
5. InputStreamReader
Contains methods to convert bytes to characters.
6. PipeReader
▪ Contains methods to read from the connected piped output stream.
7. StringReader
▪ contains methods to read from a string
• The Reader class defines various methods to perform reading operations on
data of an input stream.
• The Reader class contains methods that are identical to those available in the
InputStream class.
[ Page 14 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
5. void reset ()
▪ Resets the input pointer to the beginning of the stream.
6. long skip (long nChars)
▪ skips 'nChars' characters from the input stream and returns the number of
actually skipped characters.
7. void close ()
▪ Closes the input stream.
[ Page 15 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
Writer Classes
1. BufferedWriter
▪ Contains methods to write characters to a buffer.
2. FileWriter
▪ Contains methods to write to a file.
3. FilterWriter
▪ Contains methods to write characters to underlying output stream.
[ Page 16 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
4. CharArrayWriter
▪ Contains methods to write characters to a character array.
5. OutputStreamWriter
▪ Contains methods to convert from bytes to character.
6. PipedWriter
▪ Contains methods to write to the connected piped input stream.
7. StringWriter
▪ Contains methods to write to a string.
• The Writer class defines various methods to perform writing operations on
output stream.
[ Page 17 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
[ Page 18 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
while((x=fr.read())!=-1)
{
fw.write(x);
}
System.out.println("Data cpoy to the file successfully");
fw.close();
fr.close();
}
catch(Exception e)
{
System.out.println("Exception : " + e);
}
}
}
[ Page 19 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
1. EOFException
• Signals that an end of the file or end of stream has been reached
unexpectedly during input.
2. FileNotFoundException
• Informs that a file could not be found.
3. InterruptedException
• Warns that an I/O operation has been interrupted.
4. IOException
• Signals that an O/P exception of some sort has occurred.
✓ Each I/O statement must have an exception handler around it as shown below or
the method must declare that it throw an IOException.
try
{
…………..
}
catch(IOException e)
{
…………..
}
[ Page 20 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
DataInputStream Creation
1. Create Input File Stream:
• Example : FileInputStream fis = new FileInputStream(infile);
2. Create Input Data Stream:
• Example : DataInputStream dis = new DataInputStream( fis );
▪ The first statement creates the file input stream (fis).
▪ The second statement creates the data input stream (dis).
▪ These statements wrap dis on fis and use it as a “filter”.
▪ Note : the file object infile must be initialized with the appropriate file name
before it is used. We may also use file name directly in place of file object.
[ Page 21 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
❖ Methods Supported:
• readBoolean(), readByte(), readChar(), readShort(), readInt(), readLong(),
readFloat(), readDouble()
• They read data stored in file in binary format.
❖ Methods Supported :
• writeBoolean(), writeByte(), writeChar(), writeShort(), writeInt(),
writeLong(), writeFloat(), writeDouble() They write data to file in binary
format.
dos.close();
fos.close();
FileInputStream fis = new FileInputStream("data.txt");
DataInputStream dis = new DataInputStream(fis);
System.out.println(dis.readInt());
System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());
System.out.println(dis.readChar());
dis.close();
fis.close();
}
catch(Exception e)
{ System.out.println("Exception : " + e); }
}
}
[ Page 23 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
❖ Declaratioon :
❖ Mode
• A random access file can be created in four different access modes.
• The access mode value is a string. They are listed as follows:
Mode Meaning
"r" The file is opened in a read-only mode.
"rw" The file is opened in a read-write mode. The file is created if it does
not exist.
[ Page 24 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
Constructors
• We create an instance of the RandomAccessFile class by specifying the file
name and the access mode.
Methods
1. void close()
• Closes this random access file stream and releases any system resources
associated with the stream.
[ Page 25 of 27 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : Building Application Using Advance Java
[ UNIT – II : Managing Input /Output Files in Java ]
[ Page 27 of 27 ]