I've been trying to figure out a method to basically search within a folder for a .txt file (name of text file is based off of user input) and then spit out the contents of that text file. How would I do this via Java.io?
What I've done so far
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class searchDemo {
public static void main(String[] args) throws FileNotFoundException{
Scanner keyboard = new Scanner(System.in);
System.out.println("Who would you want to search for?");
String name = keyboard.nextLine();
File dir = new File("/Users/john/Documents/workspace/Axis Powers/users");
Scanner scan = new Scanner("/Users/john/Documents/workspace/Axis Powers/users");
String nameTweets = scan.nextLine();
for(File file : dir.listFiles()){
if(file.isFile() && nameTweets.equalsIgnoreCase(name) && nameTweets.endsWith(".txt")){
System.out.println(name);
System.out.println(nameTweets);
/**
I was getting bugs at this time so I printed the
user input first and then the ".txt" file version of
the user input to see what was printing and it
obviously wasn't what I wanted it to do
**/
}
}
}
}
Here was the task I was given: Allow a user to search up a person's name and if that person's name exists (within a file) in a .txt version then it would print out the contents the .txt file
For example: If a person searches up "John Legend" and in a file there is a file named "John Legend.txt", then it would print out the contents of the "John Legend.txt" file