Q1 What Are Input and Output Stream
Q1 What Are Input and Output Stream
InputStream OutputStream
3. InputStream consist of
method which performs:
Read next byte of data
from the input stream
and return -1 at the
3. Output Stream consists of methods
end of the file: public
which perform:
abstract int
read()throws Write a byte to current
IOException Outputstream : public void
Close current write(int)throws IOException
InputStream: public Write array of byte to current
int available()throws output stream : public void
IOException write(byte[])throws
Returns an estimate of IOException
the number of bytes Flushes the current
that can be read from OutputStream: public void
the current input flush()throws IOException
stream: public void Close current Output Stream.
close()throws : public void close()throws
IOException IOException
ByteArrayInputStream ByteArrayOutputStream
FilterInputStream FilterOutputStream
ObjectInputStream ObjectOutputStream
In these types the most In these types the most important and
important and mostly used type mostly used type is FileOutput Stream.
is FileInputStream.
QUES:- What is the difference between opening a file with constructor function and opening a file
with open() function? When in one method preferred over the other?
When an object is created then the constructor of a class initializes that object. In the same
way the file stream constructors initializes the file stream object once the fie stream objects
are created. Suppose if we want to open a file for reading the data then we make use of
Ifstream class and create its object and then we will use this input stream object for opening
the file and reading the data. For example;
The above statement creates an user defined object named frdn of input file stream class
Ifstream.This object opens the user defined filename impfile for reading the data from it.
When we have multiple files to be opened one after the other in a sequence then we can make
use of open() function. We can create the object of file stream class once and use that object
to open multiple files. If we have to open multiple files for reading it then we can follow the
below example;
frnd. close(); //This is used for terminating the association of frnd with impfile
When multiple files has to be opened using a single stream then open() function is more
preferred to be used. When single file has to be opened using single stream then opening
the file with constructor is more preferred.
QUES3:- Explain how while(fin) statement detects the end of a file that is connected to fin stream?
ANS:- To detect end of file, without using EOF(), we may check whether the stream object
has become NULL or not. For example, while(filin) { …… } The value in filin becomes zero
when the end of file is reached, and the loop ends. Since reading occurs inside the loop,
there will be no more attempts to read the file. Read more on Sarthaks.com -
https://www.sarthaks.com/436173/explain-how-while-filin-statement-detects-the-eof-for-a-
file-i-e-connected-to-filin-stream
Ques