Programming Codes For Operations On Files ISC Computer Science
Programming Codes For Operations On Files ISC Computer Science
import java.io.*;
class OperationOnFile
{
static BufferedReader b=new BufferedReader (new InputStreamReader(System.in));
Page 1
© www.javaforschool.com Operations on Text Files
/* Note: To delete or Rename a file, you need to create a File Class object pointing to
the file you want to delete or rename. */
Page 2
© www.javaforschool.com Operations on Text Files
/* Editing/Replacing a record in a File */
Page 3
© www.javaforschool.com Operations on Text Files
/* Searching for a record in a file */
/* Note: For Merging files, the files being merged must exist and contain some values
in them. In this case the 1st file must contain "Names" of few person and the 2nd file
must contain the "Pan Card number" of those persons. Both these files will be merged
into a single new file. */
Page 4
© www.javaforschool.com Operations on Text Files
/* Note: All the file names you enter for the below operations except creating a file,
must be valid and it must be present in the same directory where your programs are
saved. When you are asked for a filename, then give the complete filename along with
it's extension like "Sample.txt" */
if(ch==1)
{
System.out.print("\nEnter a File Name to Create: ");
String file= b.readLine();
System.out.print("Enter the number of names to insert: ");
int n=Integer.parseInt(b.readLine());
ob.create(file, n);
}
else if(ch==2)
{
System.out.print("\nEnter a File Name to Read from: ");
String file= b.readLine();
ob.display(file);
}
else if(ch==3)
{
System.out.print("\nEnter a File Name to Copy from: ");
String file1= b.readLine();
System.out.print("Enter the File Name to Copy to: ");
String file2= b.readLine();
ob.copy(file1,file2);
}
else if(ch==4)
{
System.out.print("\nEnter a File Name to Delete Record from: ");
String file= b.readLine();
System.out.println("\nThe file currently contains the following:");
ob.display(file);
System.out.print("Enter the name to delete: ");
String name= b.readLine();
ob.delete(file,name);
}
else if(ch==5)
{
System.out.print("\nEnter a File Name to Insert into: ");
String file= b.readLine();
System.out.println("\nThe file currently contains the following:");
ob.display(file);
System.out.print("Enter the name to Insert: ");
String name= b.readLine();
System.out.print("Enter the name after which it is to be inserted: ");
String nameafter= b.readLine();
ob.insert(file,name,nameafter);
}
Page 5
© www.javaforschool.com Operations on Text Files
else if(ch==6)
{
System.out.print("\nEnter a File Name to Edit: ");
String file= b.readLine();
System.out.println("\nThe file currently contains the following:");
ob.display(file);
System.out.print("Enter the Old name to be replaced: ");
String oldname= b.readLine();
System.out.print("Enter the New name: ");
String newname= b.readLine();
ob.edit(file,oldname,newname);
}
else if(ch==7)
{
System.out.print("\nEnter the File Name to Delete: ");
String file= b.readLine();
ob.delFile(file);
}
else if(ch==8)
{
System.out.print("\nEnter the file name to Rename: ");
String file1= b.readLine();
System.out.print("Enter the New file name: ");
String file2= b.readLine();
ob.renFile(file1,file2);
}
else if(ch==9)
{
System.out.print("\nEnter the File Name to Sort: ");
String file= b.readLine();
ob.sort(file);
}
else if(ch==10)
{
System.out.print("\nEnter the name of the file to search into: ");
String file= b.readLine();
System.out.print("Enter the name to search: ");
String s= b.readLine();
ob.search(file,s);
System.out.println("\nThe file currently contains the following:");
ob.display(file);
}
else if(ch==11)
{
System.out.print("\nEnter the name of First file: ");
String file1= b.readLine();
System.out.print("Enter the name of First file: ");
String file2= b.readLine();
System.out.print("Enter name of New file where the files will be merged: ");
String file3= b.readLine();
ob.merge(file1,file2,file3);
}
else
{
System.exit(0);
}
}
}
Page 6
© www.javaforschool.com Operations on Text Files