9919004039 java programs
9919004039 java programs
Write a Java program that reads a file name from the user, and
then displays information about whether the file exists, whether
the file is readable, whether the file is writable, the type of file
and the length of the file in bytes
Coding:
import java.io.*;
class Main
{
public static void main(String args[])
{
String filename =("mani.txt");
File f = new File("mani.txt");
System.out.println("File exists: "+f.exists());
System.out.println("File is readable: "+f.canRead());
System.out.println("File is writable: "+f.canWrite());
System.out.println("length of the file: "+f.length()+"
bytes");
try
{
char ch;
StringBuffer buff = new StringBuffer("");
FileInputStream fis = new
FileInputStream(filename);
while(fis.available()!=0)
{
ch = (char)fis.read();
buff.append(ch);
}
System.out.println("\nContents of the file are: ");
System.out.println(buff);
fis.close();
}
catch(FileNotFoundException e)
{
System.out.println("Cannot find the specified
file...");
}
catch(IOException i)
{
System.out.println("Cannot read file...");
}
}
}
2.Program to calculate area of different figures using interfaces
Coding:
interface area
{
double pi = 3.14;
double calc(double x,double y);
}
class Main
{
public static void main(String arg[])
{
rect r = new rect();
cir c = new cir();
area a;
a = r;
System.out.println("\nArea of Rectangle is : "
+a.calc(10,20));
a = c;
System.out.println("\nArea of Circle is : " +a.calc(15,15));
}
}
}
class rectangle extends shape
{
public int area_rect;
}
class triangle extends shape
{
int area_tri;