0% found this document useful (0 votes)
11 views55 pages

Java Record Final

The document is a record notebook for a Java Programming lab course undertaken by Nitish Kumar during the 2022-2023 academic year. It includes a bonafide certificate, an index of experiments, and detailed descriptions of various Java programs such as adding numbers, generating Fibonacci series, calculating factorials, and finding prime numbers. Each program section contains an aim, algorithm, program code, output, and result verification.

Uploaded by

VIJAY. R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views55 pages

Java Record Final

The document is a record notebook for a Java Programming lab course undertaken by Nitish Kumar during the 2022-2023 academic year. It includes a bonafide certificate, an index of experiments, and detailed descriptions of various Java programs such as adding numbers, generating Fibonacci series, calculating factorials, and finding prime numbers. Each program section contains an aim, algorithm, program code, output, and result verification.

Uploaded by

VIJAY. R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 55

FORM NO.

- F/ TL/ 021
Rev.00 Date 20.03.2020

RECORD NOTEBOOK

EBCS18ET1 – JAVA PROGRAMMING

DEPARTMENT
Of
COMPUTER SCIENCE AND ENGINEERING

NAME : NITISH KUMAR


REGISTER NO 211061101314
COURSE : B.TECH CSE
YEAR/SEM/SEC : II/III/C

2022-2023 (EVEN SEMESTER)


FORM NO. - F/ TL / 021
Rev.00 Date 20.03.2020

BONAFIDE CERTIFICATE

REGISTER NO : 211061101314
NAME OF LAB : BCS18ET1 – JAVA PROGRAMMING
DEPARTMENT : COMPUTER SCIENCE AND ENGINEERING

Certified that, this Record note book is a bonafide record of work done by
NITISH KUMAR of II Year B.Tech / CSE, Sec- ‘F’ in the JAVA PROGRAMMING
LAB during the year 2022-2023.

Signature of Lab-in-Charge Signature of Head of Dept

Submitted for the Practical Examination held on ------------------------------------

Internal Examiner External Examiner


INDEX

Exp. DATE Title Page No. STAFF SIGNATURE


No.

Add Two Numbers


1
2 Fibonacci Series Using For Loop

3 Fibonacci Series Using While Loop

Factorial Using For Loop


4
Factorial Using While Loop
5
6 To Find The Largest & Smallest Number

7 Swap Numbers

8 Prime Number From 1 To 100


9 Sum Of The Element Of An Array
10 Program To get input from user
11 Single Dimensional Java Array
12 Multi Dimensional java Array
13 Single Inheritance
14 Multilevel Inheritance
15 Thread
16 Multi Threading
17 Program Using Deadlock
18 File Input Stream
19 File Output Stream
20 Character Stream
EX.NO:- 1
DATE:-

ADD TO NUMBERS

AIM:-

To write a java program to add two given numbers.

ALGORITHM:-

1. Start
2. Declare variables num1, num2, and sum.
3. Initialize num1 to 5 and num2 to 15.
4. Add num1 and num2, storing the result in sum.
5. Print the sum of num1 and num2.
6. End

PROGRAM:-

Class AddTwoNumbers
{
Public static void main(String[]args)
{
int num1=5,num2=15,sum;
sum=num1+num2;
System.out.println("Sum of these numbers:"+sum);
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-2
DATE:-

AIM:-

To write a java program to print the given text.

ALGORITHM:-

1. Define a class named 'Simple


2. Declare the main method "public static void main(String[] arge)
3. print the string "Hello world" using System-out printer().

PROGRAM:-

class Simple
{
public static void main(String[]args)
{
System.out.println("HelloWorld!");
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-3
DATE:-
FIBONACCI SERIES USING FOR LOOP

AIM:-

To write a java program to get the Fibonacci series using for loop.

ALGORITHM:-

1. Start
2. Declare variables count, num1, and num2.
3. Initialize count to 7, num1 to 0, and num2 to 1.
4. Print "Fibonacci Series of 7 numbers:" to the console.
5. Start a loop from i=1 to count (inclusive).
6. Inside the loop:
- Print the current value of num1.
- Calculate the sum of the previous two numbers and store it in sumOfPrevTwo.
- Update num1 to the value of num2.
- Update num2 to the value of sumOfPrevTwo.
7. End the loop.
8. End.

PROGRAM:-

Public class JavaExample1


{
Public static void main(String[]args)
{
int count=7,num1 = 0,num2=1;
System.out.println("FibonacciSeriesof"+count+"numbers:");
for (inti= 1;i<=count;++i)
{
System.out.println(num1+"");
int sumofPrevTwo= num1+num2;
num1=num2;num2=sumofPr evTwo;
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-4
DATE:-

FIBONACCI SERIES USING WHILE LOOP

AIM:-

To write a java program to get the Fibonacci series using while loop.

ALGORITHM:-

1. Declare a class named JavaExample2.


2. Define the main method with a parameter of type String array (args).
3. Declare and initialize integer variables count to 7, num1 to 0, and num2 to 1.
4. Print "Fibonacci Series of 7 numbers:".
5. Initialize integer variable i to 1.
6. Enter a while loop that iterates until i is less than or equal to count:
-Print the value of num1.
- Calculate the sum of the previous two numbers (sumofPrevTwo) and assign it to num1.
- Assign the value of num2 to num1.
- Assign the value of sumofPrevTwo to num2.
- Increment the value of i.
7. End of the loop.

PROGRAM:-

Public class JavaExample2


{
Public static void main(String[]args)
{
int count=7,num1=0,num2=1;
System.out.println("FibonacciSeries of"+count+"numbers:");
int i=1;while(i<=count)
{
System.out.println(num1+" ");
int sumofPrevTwo=num1+num2;
num1=n um2;
num2 =sumofPrevTwo;
i++;
}
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-5
DATE:-

FACTORIAL NUMBERS USING FOR LOOP

AIM:-

To write a java program to get the factorial number using for loop.

ALGORITHM:-

1. Define class named "Java Example3"


2. Declare an integer Variable 'number' and initialize it to 5.
3. Declare an integer variable fact and initialize it to 1. This variable will store the factorial of
'number'.
4. Start a for loop with loop variable "i" and i<= number!
5. Multiply the 'fout' variable by 'i' and store the result back in the ‘fact’ variable

PROGRAM:-

public class JavaExample3


{
public static void main(String[] args)
{
int number=5;
long fact=1;
for(int i=1;i<=number;i++)
{
fact=fact*i;
}
System.out.println("Factorial of "+number+" is:"+fact);
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-6
DATE:-

FACTORIAL NUMBER USING WHILE LOOP

AIM:-

To write a java program to get the factorial number using while loop.

ALGORITHM:-

1. Declare a class named JavaExample4.


2. Define the main method with a parameter of type String array (args).
3. Declare and initialize an integer variable number to 5 to find the factorial of.
4. Declare and initialize a long variable fact to 1 to store the factorial result.
5. Initialize an integer variable i to 1.
6. Enter a while loop that iterates until i is less than or equal to number:
-Multiply fact by i and assign the result back to fact.
-Increment the value of i.
7. Print "Factorial of 5 is: " followed by the value of fact.
8. End of the program.

PROGRAM:-

Public class JavaExample4


{
Public static void main(String[]args)
{
int number=5;
long fact=1;
int i=1;
while(i<=number)
{
fact=fact*i;
i++;
}
System.out.println("Factorialof"+number+"is:"+fact);
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified..


EX.NO:- 7
DATE:-

LARGEST AND SMALLEST NUMBER

AIM:-

To write a java program to find the largest and smallest number.

ALGORITHM:-

1. Declare a class named FindLargestSmallestNumber.


2. Define the main method with a parameter of type String array (args).
3. Create an integer array numbers and initialize it with some values.
4. Initialize two integer variables smallest and largest with the first element of the array
numbers.
5. Enter a for loop starting from index 1 up to the length of the array numbers:
- Check if the current element (numbers[i]) is greater than the current largest. If true, update
largest to the value of numbers[i].
- Otherwise, if the current element (numbers[i]) is less than the current smallest, update
smallest to the value of numbers[i].
6. Print "Largest Number is: " followed by the value of largest.
7. Print "Smallest Number is: " followed by the value of smallest.
8. End of the program.

PROGRAM:-

Public class FindLargestSmallestNumber


{
public static void main(String[]args)
{
int numbers[]=newint[]{32,43,53,54,32,65,63,98,43,23};
int smallest=numbers[0];
int largest=numbers[0];
for(inti=1;i<numbers.length;i++)
{
if(numbers[i] > largest)largest=numbers[i];
else if (numbers[i] < smallest)smallest=numbers[i];
}
System.out.println("LargestNumberis:"+largest);
System.out.println("SmallestNumberis:"+smallest);
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-8
DATE:-

SWAP ELEMENTS

AIM:-

To write a java program to swap elements.

ALGORITHM:-

1. Start
2. Define a public class named "SwapElementsExample".
3. Define the main method inside the class.
Inside the main method:
- Declare and initialize two integer variables num1 and num2 with values 10 and 20 respectively.
- -Print "Before Swapping" to the console.
-Print the value of num1 to the console.
-Print the value of num2 to the console.
-Call the swap method with arguments num1 and num2.
4. Define a private static method named "swap" with two integer parameters num1 and num2.
5. Inside the swap method:
-Declare an integer variable temp and assign it the value of num1.
-Assign the value of num2 to num1.
–Assign the value of temp to num2.
–Print "After Swapping" to the console.
-Print the value of num1 to the console.
-Print the value of num2 to the console.
6. End.

PROGRAM:-

Public class SwapElementsExample


{
public static void main(String[]args)
{
int num1=10;
int num2= 20;
System.out.println("BeforeSwapping");
System.out.printl n("Value ofnum1is:"+num1);
System.out.println("Valueofnum2is: "+num2);
swap(num1,num2);
}

private static void swap(int num1,int num2)


{
int temp=num1;

num1=num 2;
num2=temp;
System.out.println("After Swapping");
System.out.println("Value of num1is:"+num1);
System.out.println("Value of num2is: "+num2);
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:- 9
DATE:-

PRIME NUMBERS

AIM:-

To write a java program to get prime numbers from 1 to 100.

ALGORITHM:-

1. Start
2. Define a public class named "PrimeNumbers".
3. Define the main method inside the class. Inside the main method:
-Declare and initialize two integer variables i and num to 0.
-Declare an empty string variable named primeNumbers to store prime numbers.
– Start a for loop with i initialized to 1 and loop until it reaches 100.
4. Declare an integer variable counter and initialize it to 0.
5. Start a nested for loop with num initialized to i and loop until it's greater than or equal to 1.
6. Check if i modulo num equals 0.
7. If true, increment counter by 1. Check if counter equals 2.
8. If true, concatenate i with a space to the primeNumbers string.
9. Print "Prime numbers from 1 to 100 are:" to the console.
10. Print the primeNumbers string to the console.
11. End.

PROGRAM:-

Class PrimeNumbers
{
Public static void main(String[]args)
{
int i=0,num=0;
String primeNumbers = "";
for(i=1;i<=100;i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
counter= counter+1;
}
if (counter==2)
{
primeNumbers=primeNumbers+i+" ";
}
}
System.out.println("Primenumbersfrom1to100are:");
System.out.println(primeNumbers);
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:- 10
DATE:-

SUM OF THE ELEMENTS OF AN ARRAY

AIM:-

To write a java program to get the sum of the given elements.

ALGORITHM:-

1. Start
2. Define a public class named "SumOfArray".
3. Define the main method inside the class.
4. Inside the main method:
- Declare an integer array named "array" containing elements 10, 20, 30, 40, 50, and 10.
- Declare an integer variable "sum" and initialize it to 0.
- Start a for-each loop to iterate over each element "num" in the array.
- Add the value of "num" to the "sum" variable.
- Print "Sum of array elements is: " followed by the value of "sum" to the console.
5. End.

PROGRAM:-

Class SumOfArray
{
Public static void main(String args[])
{
int[]array={10,20,30,40,50,10};
int sum=0;
for (int num:array)
{
sum=sum+num;
}
System.out.println("Sumofarrayelementsis:"+sum);
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-11
DATE:-

SINGLE DIMENSIONAL ARRAY

AIM:-

To write a java program to implement single dimensional array.

ALGORITHM:-

1. Start
2. Define a class named "Student" with two instance variables:
- "id" of type integer to store student ID.
- "name" of type String to store student name.
3. Define another class named "TestStudent3".
4. Define the main method inside the "TestStudent3" class.
5. Inside the main method:
- Create two instances of the "Student" class named "s1" and "s2".
- Assign values to the "id" and "name" attributes of each student:
- For "s1", set id as 101 and name as "Sonoo".
- For "s2", set id as 102 and name as "Amit".
6. Print the ID and name of each student to the console using println method:
- Print "s1.id" followed by "s1.name" with no space in between.
- Print "s2.id" followed by "s2.name" with no space in between.
7. End.

PROGRAM:-

Class Student
{
int id;
String name;
}
classTestStudent3
{
Public static void main(String args[])
{
Students1=newStudent();
Students2=new Student();
s1.id=101;
s1.name="Sonoo";
s2.id=102;
s2.name="Amit";
System.out.println(s1.id+""+s1. name);
System.out.println(s2.id+""+s2.name);
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-12
DATE:-

MULTI DIMENSIONAL ARRAY

AIM:-

To write a java program to implement multiple dimensional array.

ALGORITHM:-

1. Start
2. Define a class named "Student" with two instance variables:
- "rollno" of type integer to store the roll number.
- "name" of type String to store the name.
3. Define two methods inside the "Student" class:
- "insertRecord" method with two parameters "r" (roll number) and "n" (name):
- Assign the value of "r" to the "rollno" attribute.
- Assign the value of "n" to the "name" attribute.
- "displayInformation" method:
- Print the value of "rollno" followed by a space and then the value of "name" to the console.
4. Define another class named "TestStudent4".
5. Define the main method inside the "TestStudent4" class.
6. Inside the main method:
- Create two instances of the "Student" class named "s1" and "s2".
- Call the "insertRecord" method for "s1" with arguments 111 and "Karan".
- Call the "insertRecord" method for "s2" with arguments 222 and "Aryan".
- Call the "displayInformation" method for both "s1" and "s2".
7. End.

PROGRAM:-

class Student
{
int rollno;
String name;
void insert Record(int r, String n)
{
rollno=r; name=n;
}
void displayInformation()
{
System.out.println(rollno+" "+name);
}
}
class TestStudent4
{
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-13
DATE:-

SINGLE INHERITANCE

AIM:-

To write a java program to implement single inheritance.

ALGORITHM:-

1. Start
2. Define a class named "Animal".
3. Inside the "Animal" class:
-Define a method named "eat" with no parameters:
- Print "eating..." to the console.
4. Define a class named "Dog" which extends the "Animal" class.
5. Inside the "Dog" class:
- Define a method named "bark" with no parameters:
- Print "barking..." to the console.
6. Define another class named "TestInheritance".
7. Inside the "TestInheritance" class:
- Create an instance of the "Dog" class named "d".
- Call the "bark" method using the instance "d".
- Call the "eat" method using the instance "d".
8. End.

PROGRAM:-

Class Animal
{
Void eat()
{
System.out.println("eating...");
}
}
Class Dog extends Animal
{
Void bark()
{
System.out.println("barking...");
}
}
Class TestInheritance
{
Pubic static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-14
DATE:-

MULTILEVEL INHERITANCE

AIM:-

To write a java program to implement multilevel inheritance

ALGORITHM:-

1. Start
2. Define a class named "Animal".
3. Inside the "Animal" class:
- Define a method named "eat" with no parameters:
- Print "eating..." to the console.
4. Define a class named "Dog" which extends the "Animal" class.
5. Inside the "Dog" class:
- Define a method named "bark" with no parameters:
- Print "barking..." to the console.
6. Define a class named "BabyDog" which extends the "Dog" class.
7. Inside the "BabyDog" class:
- Define a method named "weep" with no parameters:
- Print "weeping..." to the console.
8. Define another class named "TestInheritance2".
9. Inside the "TestInheritance2" class:
- Create an instance of the "BabyDog" class named "d".
- Call the "weep" method using the instance "d".
- Call the "bark" method using the instance "d".
- Call the "eat" method using the instance "d".
10. End.

PROGRAM:-

Class Animal
{
Void eat()
{
System.out.println("eating...");
}
}
Class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
Class BabyDog extends Dog
{
Void weep()
{
System.out.println("weeping...");
}
}
classTestInheritance2
{
Public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-15
DATE:-

THREADS

AIM:-

To write a java program to implement threads.

ALGORITHM:-

1. Start
2. Define a class named "Multi3" which implements the "Runnable" interface.
3. Inside the "Multi3" class:
- Implement the "run" method required by the "Runnable" interface:
- Print "thread is running..." to the console.
4. Define the main method inside the "Multi3" class.
5. Inside the main method:
- Create an instance of the "Multi3" class named "m1".
- Create a new thread object named "t1" passing "m1" as a parameter to its constructor.
- Start the thread "t1" by invoking the "start" method.
6. End.

PROGRAM:-

Class Multi3 implements Runnable

{
Public void run()
{

System.out.println("thread is running...");
}
Public static void main(String args[])

{
Multi3m1=newMulti3();
Thread t1=new Thread(m1);
t1.start();
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-16
DATE:-

MULTIPLE THREADING

AIM:-

To write a java program to implement multi threading.

ALGORITHM:-

1. Start
2. Define a class named "MultithreadingDemo" which extends the "Thread" class.
3. Inside the "MultithreadingDemo" class Override the "run" method:
- Inside a try block:
- Print "Thread" followed by the ID of the current thread and "is running" to the console.
- Inside the catch block:
- Print "Exception is caught" to the console if any exception occurs.
4. Define another class named "Multithread".
5. Inside the "Multithread" class, define the main method.
6. Inside the main method:
- Declare an integer variable "n" and initialize it with the value 8.
- Start a loop from 0 to 7 (inclusive) using a for loop:
- Create an instance of the "MultithreadingDemo" class named "object".
- Start the thread by calling the "start" method on the "object".
7. End.

PROGRAM:-

class MultithreadingDemo extends Thread


{
Public void run()
{
Try
{
System.out.println("Thread"+Thread.currentThread().getId()+"isrunning");
}
catch(Exception e)
{
System.out.println("Exception is caught");
}
}
}
Class Multithread
{
Public static void main(String[]args)
{
int n=8;
for(inti=0;i<8;i++)
{
MultithreadingDemo object=new MultithreadingDemo();
object.sta rt();
}
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-17
DATE:-

PROGRAM USING DEADLOCK

AIM:-

To write a java program to implement program using deadlock.

ALGORITHM:-

1. Define two string resources, resource1 and resource2.


2. Create two threads, t1 and t2.
3. Define the run method for t1:
- Acquire lock on resource1.
- Print "Thread 1: locked resource 1".
- Sleep for 100 milliseconds.
- Acquire lock on resource2.
- Print "Thread 1: locked resource 2".
4. Define the run method for t2:
- Acquire lock on resource2.
- Print "Thread 2: locked resource 2".
- Sleep for 100 milliseconds.
- Acquire lock on resource1.
- Print "Thread 2: locked resource 1".
5. Start both threads (t1 and t2) simultaneously.

PROGRAM:-

class TestDeadlockExample1
{
public static void main(String[] args)
{
final String resource1 = "ratanjaiswal";
final String resource2 = "vimaljaiswal";
Thread t1 = new Thread()
{
public void run()
{
synchronized (resource1)
{
System.out.println("Thread 1: locked resource 1"); try
{
Thread.sleep(100);
}
catch (Exception e){} synchronized (resource2)
{
System.out.println("Thread 1: locked resource 2");
}
}
}
};
Thread t2 = new Thread()
{
public void run()
{
synchronized (resource2)
{
System.out.println("Thread 2: locked resource 2"); try
{
Thread.sleep(100);
}
catch (Exception e) {} synchronized (resource1)
{
System.out.println("Thread 2: locked resource 1");
}
}
}
};
t1.start();
t2.start();
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-18
DATE:-

FILE INPUT STREAM

AIM:-

To write a java program to implement

ALGORITHM:-

1. Import the FileInputStream class from the java.io package.


2. Define the main method.
3. Declare an integer variable i and initialize it to 0.
4. Try to execute the following block of code:
- Create a new FileInputStream object fin with the file path "D:\\testout.txt".
- Enter a while loop that reads bytes from the file using the read method of the FileInputStream
object fin until the end of the file (-1) is reached.
- Inside the loop:
- Read a byte from the file and store it in the variable i.
- Print the character representation of the byte using (char)i.
- Close the FileInputStream object fin.
5. Catch any exceptions that may occur during the execution of the code block and print the exception
message using System.out.println(e).

PROGRAM:-

import java.io.FileInputStream;
public class DataStreamExample
{
public static void main(String args[])
{
int i=0; try
{
FileInputStream fin=new FileInputStream("D:\\testout.txt"); while((i=fin.read())!=-1)
{
System.out.print((char)i);
}
fin.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-19
DATE:-

FILE OUTPUT STREAM

AIM:-

To write a java program to implement

ALGORITHM:-

1. Import the FileOutputStream class from the java.io package.


2. Define the main method.
3. Try to execute the following block of code:
- Create a new FileOutputStream object fout with the file path "D:\\testout.txt".
- Write the byte value 65 to the output stream using the write method of the FileOutputStream
object fout.
- Close the FileOutputStream object fout.
- Enter a try-catch block to catch any exceptions that may occur during the execution of the
code block.
4. Print "success..." to indicate that the file write operation was successful.
- Inside the catch block:
- Print the exception message using System.out.println(e).

PROGRAM:-

import java.io.FileOutputStream;
public class FileOutputStreamExample
{
public static void main(String args[])
{
fout.write(65);
fout.close();
try
{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
System.out.println("success...");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.


EX.NO:-20
DATE:-

CHARACTER STREAM

AIM:-

To write a java program to implement

ALGORITHM:-

1. Start
2. Import the necessary package `java.io.FileInputStream`.
3. Define a public class named "DataStreamExample".
4. Inside the "DataStreamExample" class, define the main method.
5. Inside the main method:
6.Open a try block. Inside the try block
- Create a new instance of `FileInputStream` named "fin" and pass the file path "D:\\
testout.txt" to its constructor.
- Call the `read` method on "fin" to read a byte from the file and store it in integer variable "i".
- Print the character representation of the byte read using `(char)i` to the console.
- Close the FileInputStream object "fin" by calling the `close` method.
7. Catch any exceptions that might occur and print the exception message to the console.
8. End.

PROGRAM:-

import java.io.FileInputStream;
public class DataStreamExample
{
public static void main(String args[])
{
try
{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=fin.read();
System.out.print((char)i);
fin.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:-

RESULT:-

Thus, the program is executed and the output is verified.

You might also like