Assignment No.
Q.1. Write a Java program to copy a file into another file and delete
first file.
Ans.:
import java.io.*;
class FileDel
public static void main(String args[]) throws Exception
for(int i=0;i<args.length;i++)
File file=new File(args[i]);
if(file.isFile())
String name=file.getName();
if(name.endsWith(".txt"))
file.delete();
System.out.println("file is deleted "+file);
else
{System.out.println(name+" "+file.length()+"
bytes");}
else
{System.out.println(args[i]+" is not a file");}
Q.2. Explain exception handling in Java .
Ans.:
An exception is an abnormal condition that arises in a code
sequence at run time. In other words, an exception is a run-
time error.
A Java exception is an object that describes an exceptional (that
is, error) condition that has occurred in a piece of code. When
an exceptional condition arises, an object representing that
exception is created and thrown in the method that caused the
error.
That method may choose to handle the exception itself, or pass
it on. Either way, at some point, the exception is caught and
processed. Exceptions can be generated by the Java run-time
system, or they can be manually generated by your code.
Java exception handling is managed via five keywords: try,
catch, throw, throws, and finally.
Syntax:
o try
o {
o // block of code to monitor for errors
o }
o catch (ExceptionType1 exOb)
o {
o // exception handler for ExceptionType1
o }
o catch (ExceptionType2 exOb)
o {
o // exception handler for ExceptionType2
o }
o // ...
o finally
o {
o // block of code to be executed before try block ends
o }
Although Java’s built-in exceptions handle most common
errors, you will probably want to create your own exception
types to handle situations specific to your applications.
This is quite easy to do: just define a subclass of Exception
(which is, of course, a subclass of Throwable). Your subclasses
don’t need to actually implement anything—it is their existence
in the type system that allows you to use them as exceptions.
Q.3. Write a program in Java to check whether the entered data is
alphabet or digit. If it is alphabet, then print whether it is capital or
small case. If digit is entered, throw user defined exception ‘Digit Not
Allowed’.
Ans.:
import java.util.*;
public class InputChecker
{
public static void main(String[ ] args)
{
System.out.print("Enter data - ");
char data = new Scanner(System.in).next( ).charAt(0);
if (Character.isLetter(data))
System.out.println("It is in "+ (Character.isLowerCase(data) ?
"Lowercase" : "Uppercase"));
else if (Character.isDigit(data))
try
{
throw new DigitNotAllowed( );
}
catch (DigitNotAllowed e)
{
System.out.println("Digit not allowed!");
}
}
}
class DigitNotAllowed extends Exception
Q.4. Write a Java program to copy alternate word from file into
another file.
Ans.:
import java.io.*;
class CopyAlternateWord
public static void main(String args[])
FileReader fr=new FileReader(“In.txt”);
FileReader fw= new FileWriter(“Out.txt”);
String str;
int i,cnt;
while((i=fr.read())!=-1)
str+=(char)i;
}
cnt=0;
for (int j=0;j<str.length();j++)
if(str[j]==” ”)
cnt++;
else if(cnt%2!=0)
continue;
else
fw.write(str[j]);
fr.close();
fw.close();
System.out.println(“Copied alternate word”);
}
Q.5. Write a java program to count occurrence of ‘is’ word in the file .
Ans.:
import java.io.*;
class GFG
{
public static void main(String args[])
{
FileInputStream f=new FileInputStream("Cfile.txt");
char ch;
int n=f.available();
String word = "is";
int count = 0;
for (int i = 0; i <n; i++)
{
ch=(char)f.read();
if (word.equals(ch))
count++;
}
System.out.println(“Count of is word is: ”+count);
}
}
Q.6. Write a java program to count number of characters, words and
lines in the given file.
Ans.:
import java.util.*;
import java.io.*;
class Cfile
{
public static void main(String args[])throws IOException
{
int nl=1,nw=0;
char ch;
FileInputStream f=new FileInputStream("Cfile.txt");
int n=f.available();
for(int i=0;i<n;i++)
{
ch=(char)f.read();
if(ch=='\n')
nl++;
else if(ch==' ')
nw++;
}
System.out.println("\nNumber of lines : "+nl);
System.out.println("\nNumber of words : "+(nl+nw));
System.out.println("\nNumber of characters : "+n);
}
}