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

Java Programming File (Mahima)

The document contains details about Java programming language including its introduction, features, structure, advantages, and disadvantages. It then provides code examples for various Java programs to demonstrate concepts like strings, math calculations, conditional statements, loops, classes and objects.

Uploaded by

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

Java Programming File (Mahima)

The document contains details about Java programming language including its introduction, features, structure, advantages, and disadvantages. It then provides code examples for various Java programs to demonstrate concepts like strings, math calculations, conditional statements, loops, classes and objects.

Uploaded by

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

DAV COLLEGE JALANDHAR

PRACTICAL FILE
OF
JAVA PROGRAMMING
LANGUAGE

SUBMITTED TO : SUBMITTED BY :
Dr. Rajeev Puri Sir Mahima
BCA-III
10722130160
2

INDEX
S.NO. TOPIC PAGE REMARKS
NO.
1. Introduction to Java 6
2. Features of Java 8
3. Structure 10
4. Advantages 11
5. Disadvantages 11
PROGRAMS
6. WAP to print a string 14
7. WAP to calculate area of circle using 14
scanner class
8. WAP to calculate arithmetic 16
expression through scanner
9. Program to show the concept of 18
conditional operator
10. Program to show the concept of 18
logical operator
11. Program to show the concept of 20
Relational operators
13. WAP to find whether a number is 20
even or odd.
14. WAP to find greatest of three 22
numbers.
15. WAP to calculate percentage and 22
division of a student
16. WAP to use Switch Statement 24
17. WAP to print Fibonacci Series. 26
18. WAP to find Factorial of a number. 26
19. WAP to calculate Sum of first n 28
numbers .
20. WAP to print a table. 28

21. WAP to calculate area of rectangle 30


using classes .
3

22. Program to show the concept of 32


Default Constructor.
23. Program to show the concept of 32
Parameterize Constructor.
24. Program to show the concept of 34
Copy Constructor.
25. Program to show the concept of 36
constructor
Overloading.
26. Program to show the concept of 38
Single Level Inheritance
27. Program to show the concept of 38
Multilevel Inheritance.
28. Program to show the concept of 40
Hierarchical Inheritance.
29. Program to how the concept of 42
overriding.
30. Program to use of Interface. 42
31. Program to use of Multiple 44
Interface
32. Program to print a pattern 44
*
**
***
****
33. Program to print a pattern 46
A
BC
DEF
GHIJ
KLMNO
34. Program to print a pattern 46
1
121
12321
1234321
123454321
35. Program to print a pattern 48
1
31
4

531
7531
97531
36. Program to use of Exception 50
handling.
37. Program with Multiple catch 50
exceptions.
38. Program to use of Stack Trace 52
39. Program to illustrate the use of final 52
keyword
40. Program to add two numbers to 54
illustrate the use of packages.
41. Program to multiply two numbers 54
to illustrate the use of packages.
42. Program to divide two numbers to 56
illustrate the use of packages.
44. Program to illustrate the use of 56
thread.
45. Program to illustrate the use of 58
constructors and methods of thread
class.
47. Program to illustrate the use of 60
IsAlive method.
49. Program to illustrate the use of 62
reading and writing files.
50. Program to illustrate the use of 64
PushBackInputStream class.
5
6

INTRODUCTION TO JAVA
Java is a class-based, object-oriented programming language
that is designed to have as few implementation dependencies
as possible. It is intended to let application developers write
once, and run anywhere (WORA), meaning that compiled Java
code can run on all platforms that support Java without the
need for recompilation. Java was first released in 1995 and is
widely used for developing applications for desktop, web, and
mobile devices. Java is known for its simplicity, robustness, and
security features, making it a popular choice for enterprise-level
applications.

JAVA was developed by James Gosling at Sun Microsystems Inc


in the year 1995 and later acquired by Oracle Corporation. It is
a simple programming language. Java makes writing, compiling,
and debugging programming easy. It helps to create reusable
code and modular programs. Java is a class-based, object-
oriented programming language and is designed to have as few
implementation dependencies as possible. A general-purpose
programming language made for developers to write once run
anywhere that is compiled Java code can run on all platforms
that support Java. Java applications are compiled to byte code
that can run on any Java Virtual Machine. The syntax of Java is
similar to C/C++.
7
8

FEATURES OF JAVA
o Simple : Java is very easy to learn, and its syntax is simple, clean
and easy to understand. Java has removed many complicated
and rarely-used features, for example, explicit pointers,
operator overloading, etc.

o Object-Oriented : Java is an object-oriented programming


language. Everything in Java is an object. Object-oriented
means we organize our software as a combination of different
types of objects that incorporate both data and behaviour.
o Secured : Java is best known for its security. With Java, we can
develop virus-free systems.
o Robust : The English mining of Robust is strong. There is a lack
of pointers that avoids security problems. There are exception
handling and the type checking mechanism in Java. All these
points make Java robust.
o Portable : Java is portable because it facilitates you to carry the
Java bytecode to any platform. It doesn't require any
implementation.
o High Performance : Java is faster than other traditional
interpreted programming languages because Java bytecode is
"close" to native code.
o Multithreaded : We can write Java programs that deal with
many tasks at once by defining multiple threads. The main
advantage of multi-threading is that it doesn't occupy memory
for each thread. It shares a common memory area. Threads are
important for multi-media, Web applications, etc.
9
10

STRUCTURE

o Documentation Section : The documentation section is an important


section but optional for a Java program. It includes basic
information about a Java program. The information includes the author's
name, date of creation, version, program name, company
name, and description of the program. It improves the readability of the
program.

o Package Declaration : The package declaration is optional. It is placed


just after the documentation section. In this section, we declare
the package name in which the class is placed.

o Import Statements : The package contains the many predefined classes


and interfaces. If we want to use any class of a particular package, we
need to import that class. We use the import keyword to import the
class.

o Interface Section : It is an optional section. We can create an interface in


this section if required. We use the interface keyword to create an
interface. An interface is a slightly different from the class. It contains
only constants and method declarations.

o Class Definition : In this section, we define the class. It is vital part of a


Java program. Without the class, we cannot create any Java program. A
Java program may conation more than one class definition. We use
the class keyword to define the class.

o Main Method Class : In this section, we define the main() method. It is


essential for all Java programs. Because the execution of all Java
programs starts from the main() method. In other words, it is an entry
point of the class. It must be inside the class. Inside the main method,
we create objects and call the methods.
11

ADVANTAGES
o Java gives Automatic Garbage Collection.
o Java is Multithreaded language.
o Memory distribution.
o Java is Secure language.
o Java is a disseminated language.
o Java code platform independent.
o Java is purely Object-Oriented programming language.

DISADVANTAGES
o Memory consumption is more.
o Less machine interactive.
o Java requires huge memory space.
o Verbose and Complex codes.
o Execution of Java language is slow.
12
13

OUTPUT - 6

OUTPUT - 7
14

6. WAP to print a String.

import java.util.*;
public class printString {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string : ");
String str = sc.nextLine();
System.out.println("Entered string is :
"+str);
}
}

7. WAP to calculate area of circle using scanner


class.

import java.util.*;
public class circleArea {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the radius of the
circle : ");
float r = sc.nextFloat();
System.out.println("Area of a circle is :
"+Math.PI*r*r);
}
}
15

OUTPUT – 8
16

8. WAP to calculate arithmetic expression through


scanner.

import java.util.*;
public class arithmeticExp
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the 1st number : ");
float n1 = sc.nextFloat();
System.out.print("Enter the 2nd number : ");
float n2 = sc.nextFloat();
System.out.print("Enter the operator(+,-,*,/,%) : ");
String op = sc.next();
switch(op){
case "+":
System.out.println("Result is : "+(n1+n2));
break;
case "-":
System.out.println("Result is : "+(n1-n2));
break;
case "*":
System.out.println("Result is : "+(n1*n2));
break;
case "/":
System.out.println("Result is : "+(n1/n2));
break;
case "%":
System.out.println("Result is : "+(n1%n2));
break;
default:
System.out.println("Invalid Operator");
}
}
}
17

OUTPUT – 9

Case:1

Case:2

OUTPUT - 10
18

9. Program to show the concept of conditional


operator.

import java.util.*;
public class condOperator {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the your age : ");
int age = sc.nextInt();
String result = (age>=18)?"You can drive":"You
cannot drive";
System.out.println(result);
}
}

10. Program to show the concept of logical operator.

public class logOperator {


public static void main(String args[]){
boolean n1 = true;
boolean n2 = false;
System.out.println("Logical AND (n1&&n2): "
+(n1&&n2));
System.out.println("Logical OR (n1||n2): "
+(n1||n2));
System.out.println("Logical NOT !(n1) : "+!(n1));
}
}
19

OUTPUT – 11

OUTPUT - 13

CASE-1

CASE-2
20

11. Program to show the concept of Relational


operators.

public class logOperator {


public static void main(String args[]){
int n1 = 10;
int n2 = 5;
System.out.println("Relational Operators");
System.out.println("n1 : "+n1+"\n"+"n2 : "+n2);
System.out.println("n1==n2 "+(n1==n2));
System.out.println("n1!=n2 "+(n1!=n2));
System.out.println("n1>=n2 "+(n1>=n2));
System.out.println("n1<=n2 "+(n1<=n2));
System.out.println("n1>n2 "+(n1>n2));
System.out.println("n1<n2 "+(n1<n2));
}
}

13. WAP to find whether a number is even or odd.

import java.util.*;
public class evenOdd {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number : ");
int num = sc.nextInt();
String res = (num%2==0)?"Even":"Odd";
System.out.println("Number is "+res);
}
}
21

OUTPUT – 14

OUTPUT – 15
22

14. WAP to find greatest of three numbers.


import java.util.*;
public class greatestThree {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter three numbers : ");
int n1 = sc.nextInt();
int n2 = sc.nextInt();
int n3 = sc.nextInt();
int res = (n1>n2)?((n1>n3)?n1:n3):((n2>n3)?n2:n3);
System.out.println("Greatest of three is
"+res);
}
}

15. WAP to calculate percentage of a student.

import java.util.*;
public class greatestThree {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter your marks in three
subjects : ");
float s1 = sc.nextFloat();
float s2 = sc.nextFloat();
float s3 = sc.nextFloat();
float percent = ((s1+s2+s3)/300)*100;
System.out.println("Percentage is
"+percent);
}
}
23

OUTPUT - 16
24

16. WAP to use of Switch Statement.

import java.util.*;
public class arithmeticExp
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the 1st number : ");
float n1 = sc.nextFloat();
System.out.print("Enter the 2nd number : ");
float n2 = sc.nextFloat();
System.out.print("Enter the operator(+,-,*,/,%) :
");
String op = sc.next();
switch(op){
case "+":
System.out.println("Result is : "+(n1+n2));
break;
case "-":
System.out.println("Result is : "+(n1-n2));
break;
case "*":
System.out.println("Result is : "+(n1*n2));
break;
case "/":
System.out.println("Result is : "+(n1/n2));
break;
case "%":
System.out.println("Result is : "+(n1%n2));
break;
default:
System.out.println("Invalid Operator");
}
}
}
25

OUTPUT - 17

OUTPUT - 18
26

17. WAP to print Fibonacci series.


import java.util.*;
public class fibonacciSeries {
public static void main(String args[]){
int a,b,c;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the limit of series : ");
int limit = sc.nextInt();
System.out.print("Fibonacci Series : ");
a=0;
b=1;
c=a+b;
System.out.print(a+" "+b);
while(c<=limit){
System.out.print(" "+c);
a=b;
b=c;
c=a+b;
}
}
}

18. WAP to find Factorial of a number.


import java.util.*;
public class fact {
public static void main(String args[]){
int f=1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number : ");
int num = sc.nextInt();
while(num>1){
f*=num;
num--;
}
System.out.println("Factorial is : "+f);
}
}
27

OUTPUT -19

OUTPUT – 20
28

19. WAP to calculate Sum of first n numbers .

import java.util.*;
public class sum{
public static void main(String args[]){
int sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the nth number : ");
int num = sc.nextInt();
for(int i=1 ; i<=num ; i++){
sum+=i;
}
System.out.println("Sum is : "+sum);
}
}

20. WAP to print a table.

import java.util.*;
public class table {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number : ");
int num = sc.nextInt();
System.out.println("Table of "+num+" : ");
for(int i=1 ; i<=10 ; i++){
System.out.println(num+" X "+i+" = "+(num*i));
}
}
}
29

OUTPUT - 21
30

21. WAP to calculate area of rectangle using classes .

import java.util.*;
class rectangle{
int l,b;
rectangle(int n1,int n2){
l=n1;
b=n2;
}
int calcArea(){
return l*b;
}
}

public class rectArea {


public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter length and
breadth of rectangle : ");
int len=sc.nextInt();
int bred=sc.nextInt();
rectangle rect = new rectangle(len,bred);
System.out.println("Area of rectangle :
"+rect.calcArea());
}
}
31

OUTPUT - 22

OUTPUT - 23
32

22. Program to show the concept of Default


Constructor.
class defaultCons{
int l,b;
void displayVal(){
System.out.println("Length is "+l);
System.out.println("Breadth is "+b);
}
}
public class constructor {
public static void main(String args[]){
defaultCons rect = new defaultCons();
rect.displayVal();
}
}

23. Program to show the concept of Parameterize


Constructor.
import java.util.*;
class rectangle{
int l,b;
rectangle(int n1,int n2){
l=n1;
b=n2;
}
int calcArea(){
return l*b;
}}
public class rectArea {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter length and breadth of
rectangle : ");
int len=sc.nextInt();
int bred=sc.nextInt();
rectangle rect = new rectangle(len,bred);
System.out.println("Area of rectangle :
"+rect.calcArea());
}
}
33

OUTPUT - 24
34

24. Program to show the concept of Copy


Constructor.

class rectangle{
int l,b;
rectangle(int n1,int n2){
this.l=n1;
this.b=n2;
}
rectangle(rectangle rect1){
l=rect1.l;
b=rect1.b;
}
}
public class constructor {
public static void main(String args[]){
rectangle rect1 = new rectangle(10,7);
rectangle rect2 = new
rectangle(rect1);
System.out.println("Length and breadth of
rectangle-1 : ");
System.out.println("Length =
"+rect1.l+"\nBreadth = "+rect1.b);
System.out.println("Length and breadth of
rectangle-2 (Copy Constructor) : ");
System.out.println("Length =
"+rect2.l+"\nBreadth = "+rect2.b);
}
}
35

OUTPUT – 25
36

25. Program to show the concept of constructor


overloading.

class box{
float l,b,h;

//for cube
box(float side){
l=b=h=side;
}
//when no side is given
box(){
l=b=h=0;
}
//all three sides are diffferent
box(float length,float breadth,float height){
l=length;
b=breadth;
h=height;
}
//display vol
float volume(){
return l*b*h;
}
}
public class constructor {
public static void main(String args[]){
box b1 = new box();
box b2 = new box(5);
box b3 = new box(2,3,4);
System.out.println("Volume of b1 : "+b1.volume());
System.out.println("Volume of b2 : "+b2.volume());
System.out.println("Volume of b3 : "+b3.volume());
}
}
37

OUTPUT - 26

OUTPUT - 27
38

26. Program to show the concept of Single Level


Inheritance.
class Employee{
int salary=80000;
}
class Engineer extends Employee{
int bonus=20000;
}
public class inheritance{
public static void main(String args[]){
Engineer e1 = new Engineer();
System.out.println("Engineer's Data");
System.out.println("Salary : "+e1.salary+"\nBonus :
"+e1.bonus);
}
}

27. Program to show the concept of Multilevel


Inheritance.
class grandparent{
void grandparentFunc(){
System.out.println("Welcome in grandparent class");
}
}
class parent extends grandparent{
void parentFunc(){
System.out.println("Welcome in parent class");
}
}
class base extends parent{
void baseFunc(){
System.out.println("Welcome in base class");
}
}
public class inheritance{
public static void main(String args[]){
base obj=new base();
obj.baseFunc();
obj.parentFunc();
obj.grandparentFunc();
}
}
39

OUTPUT - 28
40

28. Program to show the concept of Hierarchical


Inheritance.

class Employee{
int salary=80000;
}
class Engineer extends Employee{
int bonus=20000;
int totalSal(){
return salary+bonus;
}
}
class Manager extends Employee{
int bonus=10000;
int totalSal(){
return salary+bonus;
}
}
class Data_Analyst extends Employee{
int bonus=15000;
int totalSal(){
return salary+bonus;
}
}
public class inheritance{
public static void main(String args[]){
Engineer e1 = new Engineer();
Manager m1 = new Manager();
Data_Analyst da1 = new Data_Analyst();
System.out.println("Engineer's total salary :
"+e1.totalSal());
System.out.println("Manager's total salary :
"+m1.totalSal());
System.out.println("Data_Analyst's total salary :
"+da1.totalSal());
}
}
41

OUTPUT - 29

OUTPUT - 30
42

29. Program to show the concept of overriding.


class parent{
void show(){
System.out.println("This is parent class");
}
}
class base extends parent{
@Override void show(){
System.out.println("This is base class");
}
}
public class inheritance{
public static void main(String args[]){
parent obj1 = new parent();
base obj2 = new base();
obj1.show();
obj2.show();
}
}

30. Program show the use of Interface.


interface bank{
float interest();
}
class sbi implements bank{
public float interest(){
return 9.15f;
}
}
class pnb implements bank{
public float interest (){
return 9.10f;
}
}
public class interface1{
public static void main(String args[]){
bank obj1=new sbi();
System.out.println("Rate of Interest in SBI :
"+obj1.interest());
bank obj2=new pnb();
System.out.println("Rate of Interest in PNB :
"+obj2.interest());
}
}
43

OUTPUT - 31

OUTPUT - 32
44

31. Program to show the use of Multiple Interface.


interface running{
void run();
}
interface walking{
void walk();
}
class Person implements running, walking{
public void walk(){
System.out.println("Person is walking");
}
public void run(){
System.out.println("Person is running");
}
}
public class main{
public static void main(String args[]){
Person obj=new Person();
obj.walk();
obj.run();
}
}

32. Program to print a pattern : *


**
***
****
public class main{
public static void main(String[] args) {
for(int i=0;i<4;i++){
for(int j=0;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
45

OUTPUT - 33

OUTPUT - 34
46

33. Program to print a pattern : A


BC
DEF
GHIJ
KLMNO
public class main {
public static void main(String[] args) {
char ch=65;
for(int i=0;i<5;i++){
for(int j=4-i;j>0;j--){
System.out.print(" ");
}
for(int j=0;j<=i;j++){
System.out.print(ch+" ");
ch+=1;
}
System.out.println();
}
}
}

34. Program to print a pattern : 1


121
12321
1234321
123454321
public class main {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=5-i;j>0;j--){
System.out.print(" ");
}
47

OUTPUT - 35
48

for(int j=1;j<=i;j++){
System.out.print(j);
}
for(int k=i-1;k>0;k--){
System.out.print(k);
}
System.out.println();
}
}
}

35. Program to print a pattern : 1


31
531
7531
97531
public class main {
public static void main(String[] args) {
for(int i=0;i<5;i++){
for(int j=4-i;j>0;j--){
System.out.print(" ");
}
for(int j=(2*i+1);j>0;j-=2){
System.out.print(j+" ");
}
System.out.println();
}
}
}
49

OUTPUT - 36

OUTPUT - 37
50

36. Program to use of Exception handling.


import java.util.*;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
System.out.print("Enter another number: ");
int anotherNumber = sc.nextInt();
try {
int result = number / anotherNumber;
System.out.println("The result is: " + result);
} catch (ArithmeticException e) {
System.out.println("You cannot divide by zero!");
}
}
}

37. Program with Multiple catch exceptions.


import java.util.*;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
System.out.print("Enter another number: ");
int anotherNumber = sc.nextInt();
try {
int result = number / anotherNumber;
System.out.println("The result is: " + result);
} catch (ArithmeticException e) {
System.out.println("You cannot divide by zero!");
}catch (Exception e) {
System.out.println("You must enter a number!");
}
}
}
51

OUTPUT - 38

OUTPUT - 39
52

38. Program to use of Stack Trace.

import java.io.*;
class main {
public static void main(String[] args){
int a[] = { 1, 2, 3 };
try {
System.out.println(a[5]);
}catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}

39. Program to illustrate the use of final keyword.

public class main{


final int speedlimit=90;
void run(){
speedlimit=400;
}
public static void main(String args[]){
main obj=new main();
obj.run();
}
}
53

OUTPUT - 40

OUTPUT - 41
54

40. Program to add two numbers to illustrate the use


of packages.

import java.util.*;
public class main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter two numbers : ");
float a=sc.nextFloat();
float b=sc.nextFloat();
float result=a+b;
System.out.println("Sum : "+result);
}
}

41. Program to multiply two numbers to illustrate the


use of packages.

import java.util.*;
public class main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter two numbers : ");
float a=sc.nextFloat();
float b=sc.nextFloat();
float result=a*b;
System.out.println("Product : "+result);
}
}
55

OUTPUT - 42

OUTPUT - 44
56

42. Program to divide two numbers to illustrate the


use of packages.
import java.util.*;
public class main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter two numbers : ");
float a=sc.nextFloat();
float b=sc.nextFloat();
float result=a/b;
System.out.println("Divide : "+result);
}
}

44. Program to illustrate the use of thread.

public class main extends Thread {


public void run(){
System.out.println("Thread Started
Running...");
}
public static void main(String[] args){
main t1 = new main();
t1.start();
}
}
57

OUTPUT - 46
58

46. Program to illustrate the use of constructors and


methods of thread class using runnable interface.

import java.lang.Thread;
public class main {
public static void main(String[] args) {
Thread t1 = new Thread();
Thread t2 = new Thread("My Thread");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("I am a runnable
thread!");
}
});
t1.start();
t2.start();
t3.start();
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("All the threads are
finished!");
}
}
59

OUTPUT - 47
60

47. Program to illustrate the use of IsAlive


method.

public class main extends Thread{


public void run(){
try{
Thread.sleep(300);
System.out.println("is run() method isAlive
"+Thread.currentThread().isAlive());
}
catch (InterruptedException e){
System.out.println("Eroor : "+e);
}
}
public static void main(String[] args)
{
main t1 = new main();
System.out.println("before starting thread
isAlive: "+t1.isAlive());
t1.start();
System.out.println("after starting thread
isAlive: "+t1.isAlive());
}
}
61

OUTPUT - 49
62

49. Program to illustrate the use of reading and


writing files.

import java.io.*;
public class main{
public static void main(String[] args) throws
IOException{
String str = "File Handling in Java using "+"
FileWriter and FileReader";
FileWriter fw=new FileWriter("output.txt");
for (int i = 0; i < str.length(); i++)
fw.write(str.charAt(i));
System.out.println("Written successfully");
fw.close();
}
}
63

OUTPUT – 50
64

50. Program to illustrate the use of


PushBackInputStream class.

import java.io.*;
public class main {
public static void main(String[] args)throws
Exception{
String srg = "1##2#34###12";
byte ary[] = srg.getBytes();
ByteArrayInputStream array = new
ByteArrayInputStream(ary);
PushbackInputStream push = new
PushbackInputStream(array);
int i;
while( (i = push.read())!= -1) {
if(i == '#') {
int j;
if( (j = push.read()) == '#'){
System.out.print("**");
}
else{
push.unread(j);
System.out.print((char)i);
}
}
else{
System.out.print((char)i);
}
}
}
}

You might also like