Sei sulla pagina 1di 30

JAVA PROGRAMS

1) Develop a program to print greatest of 3 numbers using ternary


operator.
import java.util.*;
class Test
{
public static void main(String args[])
{
int a, b, c, greatest;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first no. ");
a = sc.nextInt();
System.out.println("Enter second no. ");
b = sc.nextInt();
System.out.println("Enter third no. ");
c = sc.nextInt();
greatest = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
System.out.println("Greatest No. : "+greatest);
}
}
2) Write a program to print all command line arguments entered by
user.
class Test
{
public static void main(String args[])
{
int i;
for(i=0; i<args.length; i++)
System.out.println(args[i]);
}
}
3) Write a program to implement different types of constructor to
perform addition of complex numbers.
class Test
{
int real, img;
Test()
{
real = 0;
img = 0;
}
Test(int real, int img)
{
this.real = real;
this.img = img;
}
Test addComplex(Test c1, Test c2)
{
Test temp = new Test();
temp.real = c1.real + c2.real;
temp.img = c1.img + c2.img;
return temp;
}
public static void main(String args[])
{
Test t1 = new Test(3, 2);
System.out.println("Complex number 1 : "+ t1.real + " + "+
t1.img+"i");
Test t2 = new Test(9, 5);
System.out.println("Complex number 2 : "+ t2.real + " + "+
t2.img+"i");
Test t3 = new Test();
t3 = t3.addComplex(t1,t2);
System.out.println("Sum of complex number : "+ t3.real + " + "+
t3.img+"i");
}
}
4) Write a program to declare a class student having variables stud_roll,
stud_name. Initialize values in constructor and display the values for
2 students.
import java.util.*;
class Student
{
int stud_roll;
String stud_name;
Student()
{
stud_roll = 0;
stud_name = "";
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter roll no. ");
stud_roll = sc.nextInt();
sc.nextLine();
System.out.println("Enter name ");
stud_name = sc.nextLine();
}
void display()
{
System.out.println("Roll No: "+stud_roll);
System.out.println("Name: "+stud_name);
}
public static void main(String args[])
{
Student s1 = new Student();
Student s2 = new Student();
s1.accept();
s2.accept();
s1.display();
s2.display();
}
}
5) Define a class person with data member as Aadharno, name, Panno
implement concept of constructor overloading. Accept data for 5
object and print it.
import java.util.*;
class Person
{
long aadharNo, panNo;
String name;
Person()
{
aadharNo = 42342342;
panNo = 12312;
name = "ABC";
}
Person(long aadharNo, long panNo, String name)
{
this.aadharNo = aadharNo;
this.panNo = panNo;
this.name = name;
}
void display()
{
System.out.println("Aadhar No: "+aadharNo);
System.out.println("Pan No: "+panNo);
System.out.println("Name: "+name);
}
public static void main(String args[])
{
long aN[] = new long[5];
long pN[] = new long[5];
String n[] = new String[5];
Scanner sc = new Scanner(System.in);
System.out.println("From Default Constructor");
Person pObj = new Person();
pObj.display();
System.out.println("From Parameterized Constructor");
Person p[] = new Person[5];
for(int i=0; i<5; i++)
{
System.out.println("Enter aadhar no. ");
aN[i] = sc.nextInt();
System.out.println("Enter pan no. ");
pN[i] = sc.nextInt();
sc.nextLine();
System.out.println("Enter name ");
n[i] = sc.nextLine();
p[i] = new Person(aN[i], pN[i], n[i]);
}
for(int i=0; i<5; i++)
p[i].display();
} }
6) Write a program to declare a class book having members title, author
and price. Declare and initialize Constructor for 2 objects and display
details of book having lowest price.
import java.util.*;
class Book
{
String title, author;
double price;
Book(String title, String author, double price)
{
this.title = title;
this.author = author;
this.price = price;
}
void display()
{
System.out.println("Title: "+title);
System.out.println("Author: "+author);
System.out.println("Price: "+price);
}
public static void main(String args[])
{
Book b1 = new Book("The Jungle Book","Rudyard Kipling",80);
Book b2 = new Book("The Adventures of Sherlock Holmes","Arthur
Conan Doyle",100);
if (b1.price < b2.price)
b1.display();
else if( b2.price < b1.price)
b2.display();
else
{
b1.display();
b1.display();
}
}
}
7) Create a class „Rectangle‟ that contains „length‟ and „width‟ as
data members. From this class drive class box which has additional
data member „depth‟ . Class „Rectangle‟ consists of a constructor
and an area ( ) function. The derived „Box‟ class have a constructor
and override function named area ( ) which returns surface area of
„Box‟ and a volume ( ) function. Write a java program calling all the
member function.
class Rectangle
{
double length, width;
Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
double area()
{
return (length * width);
}
}
class Box extends Rectangle
{
double depth;
Box(double length, double width, double depth)
{
super(length, width);
this.depth = depth;
}
double area()
{
System.out.println(super.area());
return (2 * ((length * width) + (width * depth) + (depth * length)));
}
double volume()
{
return (length * depth * width);
}
}
class Test
{
public static void main(String args[])
{
Box b = new Box(11,26,30);
System.out.println(b.area());
System.out.println(b.volume());
} }
8) Write a java program to implement multilevel inheritance with 4
levels of hierarchy.

import java.util.*;
class Student
{
int rollNo;
String name;
Student(int rollNo, String name)
{
this.rollNo = rollNo;
this.name = name;
}
}
class Marks extends Student
{
int m1, m2;
Marks(int rollNo, String name, int m1, int m2)
{
super(rollNo, name);
this.m1 = m1;
this.m2 = m2;
}
}
class Sports extends Marks
{
int score;
Sports(int rollNo, String name, int m1, int m2, int score)
{
super(rollNo, name, m1, m2);
this.score = score;
}
}
class Total extends Sports
{
int total_score;
Total(int rollNo, String name, int m1, int m2, int score)
{
super(rollNo, name, m1, m2, score);
total_score = 0;
}
void calc()
{
total_score = m1 + m2 + score;
}
void display()
{
System.out.println("Total = "+total_score);
}
}
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter roll no.");
int rollNo = sc.nextInt();
sc.nextLine();
System.out.println("Enter name ");
String name = sc.nextLine();
System.out.println("Enter m1 and m2 ");
int m1 = sc.nextInt();
int m2 = sc.nextInt();
System.out.println("Enter sports score ");
int score = sc.nextInt();
Total t = new Total(rollNo, name, m1, m2, score);
t.calc();
t.display();
}
}
9) Write a java program to implement the following.

import java.util.*;
interface I1
{
void exam();
void per_cal();
}
class Student
{
String name;
int rollNo;
int m1, m2;
}
class Result extends Student implements I1
{
double per;
public void exam()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter name: ");
name = sc.nextLine();
System.out.println("Enter roll no: ");
rollNo = sc.nextInt();
System.out.println("Enter m1 and m2: ");
m1 = sc.nextInt();
m2 = sc.nextInt();
}
public void per_cal()
{
per = (m1+m2)/2.0;
}
void display()
{
System.out.println("Name: "+name);
System.out.println("Roll no: "+rollNo);
System.out.println("m1: "+m1);
System.out.println("m2: "+m2);
System.out.println("Percentage: "+per);
}
}
class Test
{
public static void main(String args[])
{
Result r = new Result();
r.exam();
r.per_cal();
r.display();
}
}
10) Write a program to count number of occurrence of specified
character in user entered string.
import java.util.*;
class Test
{
public static void main(String args[])
{
int count = 0;
char c = ' ';
String s = new String();
Scanner sc = new Scanner(System.in);
System.out.println("Enter String:- ");
s = sc.nextLine();
System.out.println("Enter the character:- ");
c = sc.next().charAt(0);
for(int i=0; i<s.length(); i++)
if(s.charAt(i) == c)
count++;
System.out.println("No. of Occurences: "+count);
}
}
11) Write a program to demonstrate this keyword.
import java.util.*;
class Test
{
int a, b;
Test(int a, int b)
{
this.a = a;
this.b = b;
}
void display()
{
System.out.println("a = "+a);
System.out.println("b = "+b);
}
public static void main(String[] args)
{
int a, b;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 numbers: ");
a = sc.nextInt();
b = sc.nextInt();
Test t = new Test(a, b);
t.display();
}}
12) Write a program to read string and
a) Find its length
b) Reverse it
c) Display whether it is palindrome or not
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String str = new String();
StringBuffer rev;
int length;
System.out.println("Enter String: ");
str = sc.nextLine();
//Converting String to StringBuffer
StringBuffer str1 = new StringBuffer(str);

length = str1.length();
System.out.println("Length: "+length);

rev = str1.reverse();
System.out.println("Reverse: "+rev);
//Convert StringBuffer to String
String reverseString = rev.toString();
if(str.equals(reverseString))
System.out.println("String is Palindrome");
else
System.out.println("Not a Palindrome");
}
}

OR
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String str = "", rev = "";
int len;
System.out.println("Enter String: ");
str = sc.nextLine();
System.out.println("Length: "+str.length());
for(int i = str.length()-1; i>=0 ; i--)
rev += str.charAt(i);
System.out.println("Reverse: "+rev);
if(str.equals(rev))
System.out.println("It is a Palindrome");
else
System.out.println("Not a Palindrome");
}
}

13) Write a program which will read userid and password check
whether both are correct and display hello and welcome otherwise
display login failure . Also display the password in uppercase and
reverse.
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String userID = "ABC";
String pass = "abcdefgh";
String rev = "";
System.out.println("Enter User ID: ");
String enteredUserID = sc.nextLine();
System.out.println("Enter Password: ");
String enteredPass = sc.nextLine();
if(enteredUserID.equals(userID) && enteredPass.equals(pass))
{
System.out.println("Hello and Welcome");
System.out.println("Password in uppercase:
"+pass.toUpperCase());
for(int i = pass.length()-1; i>=0; i--)
rev = rev + pass.charAt(i);
System.out.println("Reversed Password: "+rev);
}
else
System.out.println("Login Failure");
}
}
14) Implement program to perform following task by using string
and stringbuffer class
a) To search a word inside a string
b) To search last position of substring
c) To compare 2 strings
d) To print reverse of the string.

import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter String1: ");
String s1 = sc.nextLine();
System.out.println("Enter String2: ");
String s2 = sc.nextLine();
StringBuffer sbr = new StringBuffer(s2);

System.out.println("\na)To search a word inside a string");


System.out.println("Enter search word: ");
String sWord = sc.nextLine();
System.out.println("String that we are considering: "+s1);
System.out.println("Search Word: "+sWord);
if(s1.contains(sWord))
System.out.println("Word Found");
else
System.out.println("Word Not Found");

System.out.println("\nb)To search last position of substring");


System.out.println("Enter substring of:- "+sbr);
String substr = sc.nextLine();
int lastPosition = sbr.lastIndexOf(substr);
System.out.println("Last Position is: "+(lastPosition+1));

System.out.println("\nc)To compare 2 strings");


System.out.println("String 1: "+s1);
System.out.println("String 2: "+s2);
if(s1.compareTo(s2) == 0)
System.out.println("Strings are equal.");
else
System.out.println("Strings are not equal.");

System.out.println("\nd)To print reverse of the string. use string buffer");


System.out.println("Original String: "+sbr);
System.out.println("Reversed String: "+sbr.reverse());
}
}
15) Perform following string/ string buffer operations, write
javaprogram.
(i) Accept a password from user
(ii) Check if password is correct then display “Good”, else display
“Wrong”
(iii) Display the password in reverse order.
(iv) Append password with “welcome”

import java.util.*;
class Test
{
public static void main(String args[])
{
String defaultPass = "abcd1234";
Scanner sc = new Scanner(System.in);
System.out.println("Enter Password: ");
String pass = sc.nextLine();
StringBuffer sbr = new StringBuffer(pass);
if(pass.equals(defaultPass))
System.out.println("Good");
else
System.out.println("Wrong");
System.out.println("Reverse Password: "+sbr.reverse());
pass += "welcome";
System.out.println("Appended Password: "+pass);
}
}

16) Write a program to find all substrings of a string and print


them. For example substring of fun are{f, fu, fun, un,n}
import java.util.*;
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String s1 = new String("");
System.out.println("Enter String: ");
s1 = sc.nextLine();
int i, j;
for(i=0; i < s1.length(); i++)
for(j=i+1; j < s1.length()+1 ; j++)
System.out.println(s1.substring(i, j));
}}
17)Write a program to create a vector to insert and display 5 elements of
different data types.
import java.util.*;
class Test
{
public static void main(String args[])
{
int i = 10;
float f = 3.14f;
double d = 134134.124124124d;
String s = "Sahethi";
char c = '$';
//Scanner sc = new Scanner(System.in);
Vector v = new Vector();
v.addElement(i);
v.addElement(f);
v.addElement(d);
v.addElement(s);
v.addElement(c);
System.out.println("Inserted Elements: ");
for(int y=0; y<v.size(); y++)
System.out.println(v.elementAt(y));
}
}
18) Write a program to create a vector with seven elements as (10,
30, 50, 20, 40, 10, 20).Remove element at 3rd and 4th position. Insert
new element at 3rd position. Display the original and current size of
vector.
import java.util.*;
class Test
{
public static void main(String args[])
{
//Scanner sc = new Scanner(System.in);
Vector v = new Vector();
v.addElement(10);
v.addElement(30);
v.addElement(50);
v.addElement(20);
v.addElement(40);
v.addElement(10);
v.addElement(20);
int ogSize = v.size();
System.out.println("Inserted Elements: ");
for(int y=0; y<v.size(); y++)
System.out.println(v.elementAt(y));
v.removeElementAt(3);
//after deletion vector size got reduced by one so the element at
4th position is not at 3 position
//if we write removeElementAt(4) then technically it removes the
5th element
v.removeElementAt(3);
System.out.println("After Deletion Elements: ");
for(int y=0; y<v.size(); y++)
System.out.println(v.elementAt(y));
v.insertElementAt(57, 3);
System.out.println("Original Size: "+ogSize);
System.out.println("Current Size: "+v.size());
}
}
19) Define an exception called „No match Exception‟ that is
thrown when a string is not equal to “MSBTE”.

import java.util.*;
class NoMatchException extends Exception
{
NoMatchException(String msg)
{
super(msg);
}
}
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
try
{
System.out.println("Enter String: ");
String s = sc.nextLine();
if(!(s.equals("MSBTE")))
throw new NoMatchException("Strings don't
match.");
}
catch (NoMatchException e)
{
System.out.println(e);
}
}
}
20) Write a program to input name and age of person and throws
user defined exception, if entered age is negative.
import java.util.*;
class NegativeException extends Exception
{
NegativeException(String msg)
{
super(msg);
}
}
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
try
{
String name = "";
int age = 0;
System.out.println("Enter Name: ");
name = sc.nextLine();
System.out.println("Enter Age: ");
age = sc.nextInt();
if(age < 0)
throw new NegativeException("Age is negative.");
}
catch (NegativeException e)
{
System.out.println(e);
}
}
}
21) Write a program to input name and balance of customer and
thread an user defined exception if balance less than 1500.
import java.util.*;
class LessBalanceException extends Exception
{
LessBalanceException(String msg)
{
super(msg);
}
}
class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
try
{
String name = "";
int bal = 0;
System.out.println("Enter Name: ");
name = sc.nextLine();
System.out.println("Enter Balance: ");
bal = sc.nextInt();
if(bal < 1500)
throw new LessBalanceException("Balance is less
than 1500.");
}
catch (LessBalanceException e)
{
System.out.println(e);
}
}
}
22) Write a program to create two thread one to print odd number
only and other to print even numbers.
class EvenThread extends Thread
{
public void run()
{
for(int i=0; i<11; i+=2)
System.out.println("Even Thread: "+i);
}
}
class OddThread extends Thread
{
public void run()
{
for(int i=1; i<10; i+=2)
System.out.println("Odd Thread: "+i);
}
}
class Test
{
public static void main(String args[])
{
EvenThread e = new EvenThread();
OddThread o = new OddThread();
e.start();
o.start();
}
}
23) Write a program to define two thread one to print from 1 to 100
and other to print from 100 to 1. First thread transfer control to
second thread after delay of 500 ms.
class Thread1 extends Thread
{
public void run()
{
try
{
for(int i=0; i<101; i++)
{
System.out.println("Thread1: "+i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
class Thread2 extends Thread
{
public void run()
{
try
{
for(int i=100; i>=0; i--)
{
System.out.println("Thread2: "+i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
class Test
{
public static void main(String args[])
{
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
t2.start(); } }
24) Write a program to set the priority of thread to display table of 2
and 5 (max priority for table of 5 and priority value 6 to print table of
2)
class TableOfFive extends Thread
{
public void run()
{
for(int i=1; i<11; i++)
System.out.println("5 * "+i+" = "+(5*i));
}
}
class TableOfTwo extends Thread
{
public void run()
{
for(int i=1; i<11; i++)
System.out.println("2 * "+i+" = "+(2*i));
}
}
class Test
{
public static void main(String args[])
{
TableOfFive t1 = new TableOfFive();
TableOfTwo t2 = new TableOfTwo();
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(6);
t1.start();
t2.start();
}
}
25) Develop a program which consists of package named
let_me_calculate with a class named calculator and a method named
add to add two integer numbers.Import let_me_calculate package in
another program with class named as Demo to add 2 numbers.
package let_me_calculate;
public class Calculator
{
public int add(int a, int b)
{
return (a+b);
}
}

import let_me_calculate.*;
import java.util.*;
class Demo
{
public static void main(String[] args)
{
Calculator c = new Calculator();
int a, b;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 no.s ");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("Addition of two numbers: "+c.add(a, b));
}
}
26) Define a package named myinstitute include class name as
department with one method to display the staff of that department.
Develop a program to import this package in a java application and
call the method defined in the package.
package myinstitute;
public class Department
{
public void display()
{
System.out.println("Dep: CO");
System.out.println("Staff: ABC");
System.out.println("Dep: CM");
System.out.println("Staff: PQR");
}
}

import myinstitute.*;
class Demo1
{
public static void main(String[] args)
{
Department d = new Department();
d.display();
}
}

27) Develop an applet to draw a human face.


import java.awt.*;
import java.applet.*;
//<applet code = "Test" Height = 400 Width = 500> </applet>
public class Test extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.drawOval(20,40,250,250);
g.drawOval(70,100,50,50);
g.drawOval(180,100,50,50);
g.drawArc(100,150,100,100,180,180);

}
}
28) Design an applet to draw and fill 5 point polygon with blue
color.
import java.awt.*;
import java.applet.*;
//<applet code = "Test" Height = 400 Width = 500> </applet>
public class Test extends Applet
{
public void paint(Graphics g)
{
int x[] = {150,100,100,200,200};
int y[] = {100,150,200,200,150};
int points = 5;
g.setColor(Color.BLUE);
g.fillPolygon(x,y,points);
}
}

29) Write an applet to accept Account No and balance in form of


parameter and print message “low balance” if the balance is less than
500.
import java.awt.*;
import java.applet.*;
//<applet code = "Test" Height = 400 Width = 500> <param name =
"accountNo" value = "12345678">
//<param name = "balance" value = "1600"></applet>
public class Test extends Applet
{
String accNo, bal;
double b;
public void paint(Graphics g)
{
accNo = getParameter("accountNo");
bal = getParameter("balance");
b = Double.parseDouble(bal);
if( b < 500)
g.drawString("Low Balance", 100, 100);
else
{
g.drawString("Account No. = "+accNo,100,100);
g.drawString("Balance = "+bal,100,120);
}
}
}
30) Design an applet to draw different shapes like cone, cube,
cylinder.
import java.awt.*;
import java.applet.*;
//<applet code = "Test" Height = 400 Width = 500> </applet>
public class Test extends Applet
{
public void paint(Graphics g)
{
//Code for Cone
g.drawLine(100,100,50,150);
g.drawLine(100,100,150,150);
g.drawOval(50,140,100,20);

//Code for Cylinder


g.drawOval(180,140,80,20);
g.drawOval(180,180,80,20);
g.drawLine(180,150,180,190);
g.drawLine(260,150,260, 190);

//Code for Cube


g.drawRect(300, 100, 100,100);
g.drawRect(330,70, 100,100);
g.drawLine(300,100,330,70);
g.drawLine(400,100,430,70);
g.drawLine(300,200,330,170);
g.drawLine(400,200,430,170);
}
}
31) Write java program to display triangle filled with red colour.
import java.awt.*;
import java.applet.*;
//<applet code = "Test" Height = 400 Width = 500> </applet>
public class Test extends Applet
{
public void paint(Graphics g)
{
int x[] = {200,100,300};
int y[] = {200,300,300};
int points = 3;
g.setColor(Color.RED);
g.fillPolygon(x,y,points);
} }
32) Design an applet to draw concentric rectangles filled with
different colors and circle inside the last rectangle.
import java.awt.*;
import java.applet.*;
//<applet code = "Test" Height = 600 Width = 700> </applet>
public class Test extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.drawRect(100,100,400,400);
g.fillRect(100,100,400,400);
g.setColor(Color.BLUE);
g.drawRect(150,150,300,300);
g.fillRect(150,150,300,300);
g.setColor(Color.PINK);
g.drawRect(200,200,200,200);
g.fillRect(200,200,200,200);
g.setColor(Color.GREEN);
g.fillOval(200,200,200,200);
}
}
33) Write a applet program to set background with red colour and
fore ground with blue colour.

import java.awt.*;
import java.applet.*;
//<applet code = "Test" Height = 500 Width = 500> </applet>
public class Test extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.RED);
setForeground(Color.BLUE);
g.drawString("Background color is RED and Foreground
color is BLUE",100,100);
}

}
34) Write an applet to accept user name in the form of parameter
and print ‘Hello<username>’.
import java.applet.*;
import java.awt.*;
//<applet code=Test width=500 height =500 >
//<param name ="username" value = "abcd"></applet>
public class Test extends Applet
{
String s;
public void init()
{
s = getParameter("username");
}
public void paint(Graphics g)
{
g.drawString("Hello"+s,50,50);

}
}
35) Design an applet which display equals size three rectangle one
below the other and fill them with orange, white and green color
respectively.
import java.util.*;
import java.awt.*;
import java.applet.*;
//<applet code=Test width=500 height =500></applet>
public class Test extends Applet
{
public void paint(Graphics g)
{
//setBackground(Color.GRAY);
g.setColor(Color.orange);
g.fillRect(50,10,180,30);
g.setColor(Color.white);
g.fillRect(50,40,180,30);
g.setColor(Color.green);
g.fillRect(50,70,180,30);
}
}
36) WAP to copy contents of one file to another using character
stream.
import java.util.*;
import java.io.*;
class Test
{
public static void main(String args[])
{
File in = new File("copy.txt");
File out = new File("example.txt");
try
{
FileReader r = new FileReader(in);
FileWriter w = new FileWriter(out);
int ch;
while((ch = r.read()) != -1)
w.write(ch);
System.out.println("Copied");
r.close();
w.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}

37) Develop a program to display the content of file supplied as


command line argument.
import java.io.*;
class Test
{
public static void main(String args[])
{
String s;
try
{
FileReader fr = new FileReader(args [0]);
BufferedReader br = new BufferedReader(fr);
while((s = br.readLine())!= null)
System.out.println(s);
fr.close();
}
catch(Exception e)
{
System.out.println(e);
}
} }
38) Write a program that will count no. of characters in a file.

import java.io.*;
class Test
{
public static void main(String args[])
{
File in = new File("copy.txt");
FileReader r = null;
int ch,count=0;
try
{
r = new FileReader(in);
while( (ch = r.read())!= -1)
count++;
System.out.println("Number of characters "+count);
r.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Potrebbero piacerti anche