You are on page 1of 2

import java.io.

*;
import java.util.Scanner;
public class index{
public static void main(String[] args) throws Exception
{
Scanner in=new Scanner(System.in);
while(true)
{
String a=in.nextLine();
int choice=Integer.parseInt(a);
switch(choice)
{
case 1:System.out.print("Enter the filename:");
String filename=in.nextLine();
File file = new File(filename);
boolean result;
try
{
result = file.createNewFile();
if(result)
{
System.out.println("File created");
}
else
{
System.out.println("File already exist at location:");
}
}
catch (IOException e)
{
e.printStackTrace(); //prints exception if any
}
break;
case 2: System.out.print("Enter the filename:");
String writefile=in.nextLine();
try{
FileOutputStream filewrite=new FileOutputStream(writefile, true); //
true for append mode
System.out.print("Enter file content: ");
String str=in.nextLine()+"\n";
byte[] b= str.getBytes();
filewrite.write(b);
filewrite.close();
System.out.println("file saved."); }
catch (IOException e)
{
e.printStackTrace(); //prints exception if any
}
break;
case 3:System.out.print("Enter the filename:");
String readfile=in.nextLine();
FileReader fileread=new FileReader(readfile);
int var;
while((var=fileread.read())!=-1)
{
System.out.print((char)var);
}
break;
case 4: System.out.print("Enter the filename:");
String del=in.nextLine();
File file1 = new File(del);
if(file1.delete())
{
System.out.println("File deleted successfully");
}
else
{
System.out.println("Failed to delete the file");
}
break;
case 5: System.out.print("Enter the filename:");
String readfile1=in.nextLine();
char tochange=in.nextLine();
char newstring=in.nextLine();
String newstr="";
FileReader fileread1=new FileReader(readfile1);
int var;
while((var=fileread1.read())!=-1)
{
int checkval=Character.compare((char)var,tochange)
if(checkval>1)
{
(char)var.replace(tochange,newstring);
newstr+=(char)var;
}
else
{
newstr+=(char)var;
}
}
break;
default:System.exit(0);
}
}
}}

You might also like