Question

(Java) write a program that searches a directory tree for a specific file

(Java) write a program that searches a directory tree for a specific file

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Code:

package main;

import java.io.*; //imports
import java.util.*;

public class Main { //main class

public static void main(String[] args) { //main function
  
   String directory, file;
   Scanner sc = new Scanner(System.in);
  
   System.out.println("Enter the directory"); //getting the directory from the user
   directory = sc.nextLine();
  
   System.out.println("Enter the file"); //getting the file
   file = sc.nextLine();
  
File folder = new File(directory); //creating a pointer to the folder
if(folder.canExecute() == false) { //checking if directory is valid
   System.out.println("Directory not Found");
   System.exit(0);
}
filesearch(file.toLowerCase(), folder); //searching for a file in folder and subfolder using recursion
  
System.out.println("File Not Found");

}

public static void filesearch(String file, File folder) {
for (File f : folder.listFiles()) { //for all files in folder

if (f.isDirectory()) { //if directory calling search recursively
   filesearch(file, f);
}

if (f.isFile()) { //if file
if (f.getName().toLowerCase().equals(file)) { //checking if name matches
System.out.println("File found in location: " + f.getAbsolutePath()); //then displaying the absolute path
System.exit(0);
}
}

}
}
  
}

Sample Output:

Enter the directory
E:\workspaces\Java\SearchFileInDirectory
Enter the file
main.java
File found in location: E:\workspaces\Java\SearchFileInDirectory\src\main\Main.java

Add a comment
Know the answer?
Add Answer to:
(Java) write a program that searches a directory tree for a specific file
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that creates and manipulates a directory of names, telephone numbers. The following...

    Write a Java program that creates and manipulates a directory of names, telephone numbers. The following information will be stored for each person in the directory: - Name (Last, First) - Home telephone number You should keep the entire collection ordered by key value (the combination of last and first names). Your program should be able to perform the following basic functions: - Search and display the contents of a particular entry - Display the entire directory - Delete an...

  • Selective Copy. Using Python program, write a program that walks through a folder tree and searches...

    Selective Copy. Using Python program, write a program that walks through a folder tree and searches for files with a certain file extension (such as .pdf or .jpg). Copy these files from whatever location they are in to a new folder. Submit the code and a screenshot of the program running in Linux

  • Write a Java program that lists all the files in a directory and their subdirectories, that...

    Write a Java program that lists all the files in a directory and their subdirectories, that mimics the Unix ls command or the Windows dir command. Note that when a directory is encountered we do not immediately print its contents recursively. Rather, as we scan each directory, place any subdirectory in a List. After the directory entries are printed then process each subdirectory recursively. For each file that is listed, include the modification time, the file size, and if it...

  • Write the program in Java. Description: Write a program to calculate the total size of all...

    Write the program in Java. Description: Write a program to calculate the total size of all files in the current directory / folder and all sub-folders. Example: I have a directory with 1 file (size 150KB) and 2 subdirectories, each of which has 1 file (size 10KB each), then when I run this program on this directory, I expect the answer to be 170KB.

  • Can you write with comments please. Thank you. Write a Java Program that asks the user...

    Can you write with comments please. Thank you. Write a Java Program that asks the user to enter the responses on the Console and write the responses to a text file called responses.txt (in the same directory as the program) Write another JAVA prorgram to read responses.txt file and calculate the frequency of each rating in the file and output the number and frequency in two columns (number, frequency) on the Console.

  • java Program Write a program called Copy that accepts the names of an input file and...

    java Program Write a program called Copy that accepts the names of an input file and an output file on the command line and copies the contents of the input file to the output file, for example, csc$ java Copy infile.txt outfile.txt

  • Write a java program for creating a binary search tree from an empty tree where you...

    Write a java program for creating a binary search tree from an empty tree where you must consider 10 data items to form the tree.

  • Program Description: (C++). Write a word search program that searches an input data file for a...

    Program Description: (C++). Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of characters in the input data file that are not vowels. Your program must do this by providing the following functions : void processFile(ifstream &inFile, string &word, int &wordCount, int &nonVowelCount) ; void...

  • 1) Write a java program to create a tree and apply the preorder, in-order and postorder...

    1) Write a java program to create a tree and apply the preorder, in-order and postorder traversal of the data items of the tree

  • Reading and Writing Files in a Directory Create a program that provides a listing of all...

    Reading and Writing Files in a Directory Create a program that provides a listing of all the files in a directory. The program should be a Java application that accepts a single argument into the args array of the main() method. This argument identifies the name of a directory. The application should make sure that this filename really does identify a directory and then list all of the files in the directory. For each file, list whether the file is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT