You are on page 1of 3

IMPLEMENTATION OF FILE HANDLING

AIM
PROGRAM:
import java.io.*;
class files
{
String s[]=new String[100];
String str="",m,t;
int x,size=0,i,tem,j=0,chg;
int a[]=new int[100];
int b[]=new int[100];
int c[]=new int[100];
files(String s1,String s2)throws IOException
{
File r=new File(s1);
File w=new File(s2);
FileInputStream fin=new FileInputStream(r);
FileOutputStream fout=new FileOutputStream(w);
do{
i=fin.read();
if(i!=-1)
{
str=str+(char)i;
if((char)i=='\n')
{
s[j]=str;
j++;
size++;
str="";
}
}
}
while(i!=-1);
for(int l=0;l<size;l++)
{
if(s[l]==null)
break;
m=s[l];
t="tata";
b[l]=a[l]=search(m,t);
}
for(int p=0;p<size;p++)
{
for(int q=0;q<size;q++)
{
if(b[q]<b[q+1])
{
tem=b[q];
b[q]=b[q+1];
b[q+1]=tem;

}
}
}
for(int h=0;h<size;h++)
{
x=b[h];
c[h]=linear(x,a,size);
}
for(int y=0;y<size;y++)
{
chg=c[y];
byte buf[]=s[chg].getBytes();
fout.write(buf);
}
}
static int linear(int x,int a[],int size)
{
int m,d,item=x,siz=size;
for(d=0;d<=siz;d++)
{
if(item==a[d])
{
m=d;
break;
}
}
return d;
}
static int search(String m,String c)
{
int ch,ma,inc=0;
String pat,main,check;
pat=main=m;
check=c;
do{
pat=main.substring(0,4);
if(pat.equalsIgnoreCase(check))
++inc;
main=main.substring(1);
ch=check.length();
ma=main.length();
}
while(ch<ma);
return inc;
}
}
class filehand
{
public static void main(String a[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);

String s1,s2;
System.out.println("enter the read file name :");
s1=in.readLine();
System.out.println("enter the write file name:");
s2=in.readLine();
files f=new files(s1,s2);
System.out.println("successfully writed");
}
}

You might also like