0% found this document useful (0 votes)
14 views

OOPS With Java Unit 2

Java assignment

Uploaded by

aniketgupta3625
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

OOPS With Java Unit 2

Java assignment

Uploaded by

aniketgupta3625
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

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.
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.
Control Flow in Exceptions
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.
In Exception handling , we should have
an alternate source through which we
can handle the Exception.
Input / Output Basics
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.
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
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 :
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.


Multithreading

You might also like