
Prompt the user to enter the name of a text file to search for. If the name does not end in a .txt extension, display an error message. Search for this file in the current directory. If the file does not exist print a message that says so.

The given code prompt the user to enter the name of a text file to search for. If the name does not have a .txt extension, display an error message. Then search the file in the current directory. If the file does not exist in the current directory then also display an error message.
import java.io.*;
import java.util.*;
class FileHandling
{
public static void main(String[] args)
{
try{
Scanner input=new Scanner(System.in);
System.out.print("Enter name of file: ");
String fileName=input.nextLine();
int mid= fileName.lastIndexOf(".");
String fname=fileName.substring(0,mid);
String ext=fileName.substring(mid+1,fileName.length());
File directory=new File(".");
if(ext.equals("txt")){
String st=directory.getCanonicalPath();
String file=st+"/"+fileName;
File f=new File(file);
if(!f.exists()){
System.out.println("File does not exists!!!!");
}
}
else {
System.out.println("File does not have text extension!!!!!");
}
}
catch(Exception e){}
}
}