Documentos de Académico
Documentos de Profesional
Documentos de Cultura
11. Explain the various rules for naming a variable in Java? Give one example for each.
12. What are the different methods under Buffered Input Stream and Buffered Output
Stream? Explain the uses of random access file over sequential access file?
The DataInputStream class provides methods that are complementary to the methods of
DataInputStream classes. It keeps track of the number of bytes written to the output
stream.
ROLL-NO-510286414 CENTRE-01976
NAME:-AHUJA PRAKASH DHANRAJ
MBAN-SEM-IV MI0032- SETI
The term random access means that data can be read from or written to random locations
within a file. In all the above mentioned streams, data is read and written as a continuous
stream of information. Java provides the RandomAccessFile class to perform I/O
operations a t specified locations within a file.
The RandomAccessFile class implements the DataInput and DataOutput interfaces for
performing I/O using the primitive data types. The RandomAccessFile class also supports
permissions like read and write, and allows files to be accessed in read-only and read-
write modes.
Creating a Random Access File
There are two ways to create a random access file- using the pathname as a string or
using an object of the File class.
RandomAccessFile (String pathname, String mode);
RandomAccessFile (File name, String mode);
Example
randomFile = new RandomAccessFile (“iotets.txt”,”rw”);
or
File file1 = new File (“iotest.txt”);
RandomAccessFile randomFile = new RandomAccessFile
(file1,”rw”);
The second argument is the mode argument that determines whether you have read-only
or read/write (rw) access to the file. The RandomAccessFile class has several methods
that allow random access to the content within the file.
Method Explanation
void seek (long pos Sets the file pointer to a particular location inside
) the file.
long getFilePointer
Return the current location of the file pointer.
()
long length () Returns the length of the file, in bytes.
ROLL-NO-510286414 CENTRE-01976
NAME:-AHUJA PRAKASH DHANRAJ
MBAN-SEM-IV MI0032- SETI
13. With suitable example, explain the difference between errors and exceptions. Explain
the various types of exceptions.
Ans. The term exception denotes an exceptional event. It can be defined as an abnormal
event that occurs during program execution and disrupts the normal flow of instruction.
Error-handling becomes a necessity when you develop applications that need to take care
of unexpected situations. The unexpected situations that may occur during program
execution are:
• Running out of memory
• Resource allocation errors.
• Inability to find a file.
• Problems in network connectivity.
If an above-mentioned situation is encountered, a program may stop working. You cannot
afford to have an application stop working or crashing, if the requested file is not present
on the disk. Traditionally, programmers used return values of methods to detect the errors
that occurred at runtime. A variable errno was used for a numeric representation of the
error. When multiple errors occurred in a method, errno would have only one value-that
of the last error that occurred in the method.
The most common exceptions that you may encounter are described below.
• Arithmetic Exception
This exception is thrown when an exceptional arithmetic condition has occurred. For
example, a division by zero generates such an exception.
• NullPointer Exception
This exception is thrown when an application attempts to use null where an object is
required. An object that has not been allocated memory holds a null value. The situations
in which an exception is thrown include:
• Using an object without allocating memory for it.
• Calling the methods of a null object.
• Accessing or modifying the attributes of a null object.
• ArrayIndexOutOfBounds Exception
The exception ArrayIndexOutOfBounds Exception is thrown when an attempt is made to
access an array element beyond the index of the array. For example, if you try to access
the eleventh element of an array that’s has only ten elements, the exception will be
thrown.
ROLL-NO-510286414 CENTRE-01976
NAME:-AHUJA PRAKASH DHANRAJ