Files and streams
Introduction
All computer programs must accept input and generate output. Java
Features a rich set of classes that represent everything from a general
Input or output stream to a sophisticated random-access file
What are files?
File are used to store large amounts of data for a long period of time
Specially, random-access files are used to read from and write to any
Location in the file
What are streams?
Streams, on the other hand, merely refers to the flow of data.
It represents a connection to a communications channel. There are
Input streams, which direct data from outside world to the
Computer, and output streams, which direct data toward output
Devices, such as the computer screen or a file
The following are the classes in the java.i0 package:
Input Stream Output Stream Input Stream Output Stream Buffered Input Stream Buffered Output Stream Data Input Stream Data Output Stream File Input Stream File Output Stream Byte Input Stream Byte Output Stream String Buffer Input Stream Piped Output String Line Number Input Stream Print Stream Push Back Input Stream Piped Input Stream Sequence Input Stream |
What are Random-Access Files?
The Random Access File Class provides standard file handling which
Allows opening, closing, reading, and writing files the general
Technique is to open a file by creating a new Random-access File object
With a filename as a parameter then read the file into an array of bytes
The byte array can then be manipulated and written back to the file
Random-access files are appropriate for "instant-access" application.
Practical application include: airline reservation systems, banking systems
POS systems, automated teller machines, etc.
Exceptions
An exception is an occurrence, usually some form of error, that happen
While a program is executing the normal flow of execution is disrupted
And transferred to code which can handle the exceptional condition
The exception mechanism is a lot cleaner than having to check an error
Value after every method call which could potentially fail
Java defines many different exception object some of these you must
Always handle in your code if you call a method that may throw the
Exception .others are generated by the system when something like
Allocation fail, an expression tries to divide by 0, a null is used
Inappropriately, and so no
Common Java Exception
Arithmetic Exception Cause by math errors such as division by zero
Array Index Out of Bounds Exception Cause by bad array indexes
Array Store Exception Cause when a program tries to store the wrong
Type of data in an array
File not found Exception Cause by an attempt to access a nonexistent file
IO Exception Cause by general I/O failures, such as inability
To read from a file
Null Pointer Exception Cause by referencing a null object
Number Format Exception Cause when a conversion between strings
And numbers fails
Out Of Memory Exception Cause when there is not enough memory to
Allocated a new object
Security Exception Cause when an applet tries to perform an action
Not allowed by the browser’s security setting
Stock Over Flow Exception Cause when the system runs out of stock space
String Index Out of Bounds Cause when a program attempts to access a
Exception
Nonexistent character position in string
Class Throw able.
Constructions: Throw able ( )
Throw able (String message)
Methods: Throw able Fill in Stack Track ( )
Void Print Stack Trace ( )
Void Print Stack Trace (print stream out)
String Get Message ( )
String to String ( )
Error
Serious error in the java runtime system. You should never catch this type
Of exception and in a correct system they should , on the whole, not occur
The major exception to this is the Thread Death exception, which is a special
Case.
Exception
Normal errors that can occur during the execution of a program .
They include problems such as array bound errors and floating point.
Exception . These exceptions are usually handled by code block.
Runtime Exceptions
Encompasses all exceptions which can ordinarily happen at runtime.
Generating Exception
Exception are generated in several ways, namely:
Method calls
If a method or constructor is declared to throw an exception, them calling
that method or constructor may result in an exception of declared class or a
subclass. It is a compile-time error for a method body to throw , and not
catch, an exception that is not declared in the throws clause, unless it is a
Runtime exception.
int method ( ) throws IOexception {
…..
}
method ( ) ; // this can throw an exception
Runtime exception
These exception can occur even through the offering piece of code does not
declared that it throws such an exception .Examples of these exceptions include:
Null pointer Exception, Array Index Out of Bound Exception , etc..
These exception can be thrown by a language statement or a method call;
The method executes a statement which throws the exception, and the method
Does not