6th Cs
6th Cs
Since the steams are unidirectional, they can transmit bytes in only one direction and,
The InputStream includes methods that are designed to perform the following tasks:
Reading bytes
Closing streams
Making position in the streams
Finding the number of bytes in stream
Methods of InputStream & FileInputStream class
2. Output Stream classes:
Output Stream classes are used to write 8-bit bytes
Output Stream classes are derived from base class OutputStream.
Like InputStream, the OutputStream is an abstract class and therefore we cannot instantiate it.
The several subclasses of the OutputStream can be used for performing the output operations.
The OutputStream includes methods that are designed to perform the following task:
1. Writing bytes
2. Closing streams
3. Flushing streams
(e) Explain Arrary list & Iterator methods of collections with an example.
Ans:
Methods of ArrayList class :
a. Inserts the specified element at the specified position index in this list.
b. Throws IndexOutOfBoundsException if the specified index is out of
range (index < 0 ||index >size()).
2. boolean add(Object o) :
Appends the specified element to the end of this list.
3. boolean addAll(Collection c) :
a. Appends all of the elements in the specified collection to the end of this list, in the
order that they are returned by the specified collection's iterator.
b. ThrowsNullPointerException if the specified collection is null.
5. void clear() :
Removes all of the elements from this list.
6. Object clone():
Returns a shallow copy of this ArrayList.
7. boolean contains(Object o) :
a. Returns true if this list contains the specified element.
b. More formally, returns true if and only if this list contains at least one element e such that
(o==null ? e==null : o.equals(e))
1. booleanhasNext( ) :
Returns true if there are more elements. Otherwise, returns false.
2. Object next( ):
Returns the next element. Throws NoSuchElementException if there is not a next element.
3. void remove( ):
a. Removes the current element.
b. Throws IllegalStateException if an attempt is made to call remove( ) that is not preceded
by a call to next( ).
(c) Write any four methods of File Input stream class give their syntax.
Ans.:
Java File Input Stream class methods:
Summer 2017
(b) Write any four methods of file class with their use. (Note:
Any four methods may be considered)
Ans.:
1) public String getName():
Returns the name of the file or directory denoted by this abstract pathname.
(c) What is Iterator class? Give syntax and use of any two
methods of Iterator class.
Ans.:
Iterator enables you to cycle through a collection, obtaining or removing elements. Each of the
collection classes provides an iterator( ) method that returns an iterator to the start of the collection.
By using this iterator object, you can access each element in the collection, one element at a time
Syntax :
Iterator iterator_variable = collection_object.iterator();
Methods:
1. Boolean hasNext( ):
Returns true if there are more elements. Otherwise, returns false.
2. Object next( ):
Returns the next element. Throws NoSuchElementException if there is not a next element.
3.void remove( ):
Removes the current element. Throws IllegalStateException if an attempt is made to call remove( )
that is not preceded by a call to next( ).