1. Write a program that demonstrate File
class.
import java.io.*;
import java.util.*;
class Filedemo
{
public static void main(String a[])
{
File f1=new File("E:/java");
File f2=new File(f1,"japp1.java");
System.out.println("path
"+f2.getPath());
System.out.println("name
"+f2.getName());
System.out.println("size
"+f2.length());
System.out.println("directory
"+f1.isDirectory());
System.out.println("file
"+f2.isFile());
System.out.println("exists "+f2.exists());
System.out.println("Parent:
"+f2.getParent());
long t = f2.lastModified();
Date dt= new Date(t);
System.out.println("LastModified:
"+dt );
String list[]=f1.list();
int n=list.length;
for(int i=0;i<n;i++)
{
if(list[i].charAt(0)=='u')
{
System.out.println(list[i]);
}
}
}
}
2. Write a java program to write 1 to 10 in
one file, read the same file and copy even numbers in even file and odd numbers
in odd file. Read both, even and odd files.
import java.io.*;
public class eveodd
{
public static void main(String args[])
{
try
{
FileOutputStream fw =new FileOutputStream("first4");
for(int i=1;i<=10;i++)
fw.write(i);
fw.close();
}
catch(Exception e){ }
try
{
FileOutputStream fw=new FileOutputStream("even");
FileOutputStream fw1=new FileOutputStream("odd");
FileInputStream fr=new FileInputStream("first4");
int j;
while((j=fr.read())!=-1)
{
if(j%2==0)
fw.write(j);
else
fw1.write(j);
}
fr.close();
fw.close();
fw1.close();
}
catch(Exception e){ }
try{
FileInputStream
fr=new FileInputStream("even");
FileInputStream fr1=new FileInputStream("odd");
int k;
System.out.println("Even data");
while((k=fr.read())!=-1)
System.out.println(k);
System.out.println("Odd data");
while((k=fr1.read())!=-1)
System.out.println(k);
fr.close();
fr1.close();
}
catch(Exception e){}
}
}
3. Write a program that describes how to write
objects to the file and read object from the file.
import java.io.*;
class Biodata implements Serializable
{
String n;
int
age;
Biodata(){ }
Biodata(String n1, int a1)
{
n=n1;
age=a1;
}
void
print()
{
System.out.println("name"+n);
System.out.println("age"+age);
}
}
class ObjDemo
{
public static void main(String args[])
{
try
{
FileOutputStream fos=new FileOutputStream("obj1.dat");
ObjectOutputStream ob=new ObjectOutputStream(fos);
Biodata m2=new Biodata("ghi",3);
ob.writeUTF(m2.n);
ob.writeInt(m2.age);
ob.flush();
ob.close();
Biodata b1;
Biodata b2= new Biodata();
ObjectInputStream obj1= new ObjectInputStream(new
FileInputStream("obj1.dat"));
while(true)
{
b2.n=(String)obj1.readUTF();
b2.age=(int)obj1.readInt();
b2.print();
if(b2.age==3)
break;
}
obj1.close();
}
catch(Exception e)
{
System.out.println("excetion"+e.getMessage());
}
}
}
4. Write a program to write object to the file
and read object from the file.
import java.io.*;
class Biodata implements Serializable
{
String n;
int
age;
Biodata(){ }
Biodata(String n1, int a1)
{
n=n1;
age=a1;
}
void
print()
{
System.out.println("name"+n);
System.out.println("age"+age);
}
}
class ObjDemo1
{
public static void main(String args[])
{
try
{
FileOutputStream fos=new FileOutputStream("obj1.dat");
ObjectOutputStream ob=new ObjectOutputStream(fos);
Biodata m=new Biodata("abc",1);
Biodata m1=new Biodata("def",2);
ob.writeObject(m);
ob.writeObject(m1);
ob.flush();
ob.close();
Biodata b1;
int i=0;
ObjectInputStream obj1= new ObjectInputStream(new
FileInputStream("obj1.dat"));
while(true)
{
b1=(Biodata)obj1.readObject();
b1.print();
i++;
if(i==2)
break;
}
obj1.close();
}
catch(Exception e)
{
System.out.println("exception"+e.getMessage());
}
}
}
5. Write a program that describes, how to
access data randomly from the file.
import java.io.*;
class RandDemo
{
public static void main(String args[])
{
File f=new File("ran.txt");
int a=123,a1,a2;
String s="abcdef",s1,s2;
long l=9876,l1,l2;
long ap,lp,sp;
try
{
RandomAccessFile rf=new RandomAccessFile(f,"rw");
rf.writeInt(a);
rf.writeUTF(s);
rf.writeLong(l);
rf.close();
rf=new RandomAccessFile(f,"rw");
ap=rf.getFilePointer();
a1=rf.readInt();
sp=rf.getFilePointer();
s1=rf.readUTF();
lp=rf.getFilePointer();
l1=rf.readLong();
System.out.println("sequntial
data:");
System.out.println(" "+a1+s1+l1);
//----------random reading------------
rf.seek(ap);
a2=rf.readInt();
rf.seek(lp);
l2=rf.readLong();
rf.seek(sp);
s2=rf.readUTF();
System.out.println("random
data:");
System.out.println(" "+a2+s2+l2);
rf.close();
}
catch(IOException e)
{
System.out.println(" "+e.getMessage());
}
}
}
6. Write a program that demonstrate use of
SequenceInputStream class. [f1.txt and f2.txt files are already exists.]
import java.io.*;
class sequencedemo
{
public static void main(String args[]) throws IOException
{
FileInputStream f1= new FileInputStream("f1.txt");
FileInputStream f2= new FileInputStream("f2.txt");
SequenceInputStream s=new SequenceInputStream(f1,f2);
int ans;
while((ans=s.read())!=-1)
System.out.print((char)ans);
f1.close();
f2.close();
s.close();
}
}
7. Write a program that describes keyboard
input.
import java.io.*;
public class std1
{
public static void main(String args[]) //throws Exception
{
DataInputStream dis =new DataInputStream(System.in);
int i;
float f;
char c;
String s;
try{
System.out.println("enter integer");
i=Integer.parseInt(dis.readLine());
System.out.println("enter float");
f=Float.valueOf(dis.readLine()).floatValue();
System.out.println("enter string");
s=dis.readLine();
System.out.println("enter character");
c=(char)dis.read();
System.out.println("integer
"+i);
System.out.println("character
"+c);
System.out.println("float
"+f);
System.out.println("string
"+s);
}
catch(Exception e){}
}
}