0% found this document useful (0 votes)
4 views45 pages

Unit 2 Oops

The document covers Exception Handling in Java, explaining that exceptions are events disrupting the normal flow of a program, while errors are serious issues that cannot be recovered from. It details the control flow of exceptions in the Java Virtual Machine (JVM) and provides an overview of reading and writing files using FileInputStream and FileOutputStream classes. Additionally, it includes example code for both reading from and writing to files in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views45 pages

Unit 2 Oops

The document covers Exception Handling in Java, explaining that exceptions are events disrupting the normal flow of a program, while errors are serious issues that cannot be recovered from. It details the control flow of exceptions in the Java Virtual Machine (JVM) and provides an overview of reading and writing files using FileInputStream and FileOutputStream classes. Additionally, it includes example code for both reading from and writing to files in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

lOMoARcPSD|51548381

OOPS with Java Unit 2

B.Tech CSE (2nd Year) (Dr. A.P.J. Abdul Kalam Technical University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Priyanshu ([email protected])
lOMoARcPSD|51548381

Object Oriented Programming with Java


Unit :- 2
Exception Handling

An exception is something that is left out or not done on


purpose. An exception to a rule does not follow that rule.

In Java “an event that occurs during the execution of a


program that disrupts the normal flow of instructions” is called an
exception. This is generally an unexpected or unwanted event which
can occur either at compile-time or run-time in application code.

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

In Short errors and exceptions represent different types of problems that


can occur during program execution. Errors are usually caused by serious
problems that cannot be recovered from, while exceptions are used to
handle recoverable errors within a program.

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Control Flow in Exceptions

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

JVM Reaction to Exceptions


The JVM starts the search from the method where the exception
occurred and then goes up the call stack, checking each method in
turn for a catch block that can handle the exception. If a catch block
is found, the JVM transfers control to that block and the exception is
considered to be handled.

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

In Exception handling , we should have


an alternate source through which we
can handle the Exception.

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Input / Output Basics

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Reading and Writing File in Java


1. In Java, the FilelnputSream class is used for reading binary data
Reading from a file: from a file.
2. It is an input stream that reads bytes from a file in a file system.

3.To read data from a file using FileInputStream, these steps you
need to follow :-
Step 1: Create an instance of the FileInputStream class and pass the path of the file that you
want to read as an argument to its constructor.

Step 2: Create a byte array of a fixed size to read a chunk of data from the file.

Step 3: Use the read() method of the FileInputStream class to read data from the file into the
byte array. This method returns the number of bytes read , or -1 if the end of the file
has been reached.

Step 4: Continue reading data from the file until the read() method returns -1.

Step 5: Close the FileInputStream object using to release the close() method to release any
system resources associated with it.

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Following example demonstrates how to use Fileinputstream


to read data from a file :
import java.io.*
In the example above, we create a
public class ReadFileExample {
FilelnputStream object to read data
public static void main(String[] args) {
from a file called "file.txt".
try {
FileInputStream fileinput = new
We then create a byte array of size 1024
FileInputStream ("file.txt");
to read a chunk of data from the file.
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileInput.read(buffer))!=-1){
System.out.println/new String buffer, 0, bytesread));
} We use the read() method to read data
fileInput.close(); into the buffer untill the end of the file is
} catch (IOException e) { reached, and then we close the
e.printStackTrace(); FilelnputStream object.
}
} Finally, we print the data that we read
} from the file to the console

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Writing in a file: 1. In Java, FileOutputStream class in used for writing binary data to a file.

2. It is an output stream that writes bytes to a file in a file system.

3. To write data to a file using FileOutputStream, you need to follow


these steps:
Step 1: Create an instance of the FileOutputStream class and pass the path of the file that
you want to write as an argument to its constructor. If the file dosen't exist, it will
be created automatically.

Step 2: Create a byte array that contains the date that you want to write to the file.

Step 3: Use the write() method of the FileOutputStream class to write the data to the File
This method writes the entire array to the the file.

Step 4: Close the FileOutputStream object using the close() method to release any system
resources associated with it.

Following example demonstrates how to use FileOutputstream


to write data to a file :

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

import java.io.*;
public class Write File Example {
public static void main(String[] args) {
try {
FileOutputStream fileOutput = new
FileOutputStream("file.txt");
String data ="This is some data that will be written to a file";
byte[] bytes = data.getBytes();
fileOutput.write(bytes);
In the example above, we create a FileOutputStream
fileOutput.close();
object write data to a file called "file.txt".
} catch (IOException e) {
e.printStackTrace();
We then create a string that contains some data that we
}
wants write to the file.
}
}
We convert this string to a byte array using the getbytes()
method and then we use the write() method to write the
data to the file.

Finally, we close the FileOutputStream object.

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Multithreading

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])


lOMoARcPSD|51548381

Downloaded by Priyanshu ([email protected])

You might also like