Solved programming problem

Search a File of a Directory in Java.

Source code

package pkg3;

import java.util.Scanner;

import java.io.File;

public class Main

{

    public static void main(String[] args)

    {

       Scanner sc = new Scanner(System.in);

     

        System.out.println("File Name: ");

        String name = sc.nextLine();

     

       System.out.println("Enter the path: ");

       String str = sc.nextLine();

     

       File  f = new File(str);

       File total[] = f.listFiles();

               

        for(int i =0;i<total.length;i++)

        {

            if(total[i].getName().equals(name))

            {

               System.out.println("Found!!!
File Location: "+total[i].getAbsoluteFile());

               System.exit(0);

            }

        }

        System.out.println("Not Found");

     

    }

 

}

Output

File Name:

srs

Enter the path:(your file)

 

 

Join the discussion

Add your comment