-1

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

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • There's an inconsistency. The task says _"if that person's name exists (within a file)"_ implying that you have to search the contents of the file. You say you are looking only for a file whose name matches the person. These are very different tasks. Please clarify which one you mean. Also, you are attempting to open a `Scanner` on a directory, which won't work. Finally, you haven't explained what problem you're having. – Jim Garrison Nov 20 '17 at 00:22
  • @JimGarrison ahhh! Sorry I meant "if a person'a name exists (within a folder) in a .txt file version...." & also at the bottom for the example; I meant "if a person searches up [John Legend] and in a FOLDER there is a FILE named [John Legend.txt] it would print out the contents of the [John Legend.txt] FILE" The problem I'm having is that I'm not quite sure what to do so that name (from user input) & nameTweets (.txt file) are the same so that the contents of the .txt file would be printed – Sweetcharge Nov 20 '17 at 00:28
  • What problems are you encountering? Please visit the [help] and read [ask] to learn the guidelines for using this site. Show your error messages and if you have a stack trace make sure to include the COMPLETE stack trace, including all "Caused By" sections, and indicate which statement in your code threw the exception. Format the stack trace as code (indent 4 spaces or use the `{}` button) – Jim Garrison Nov 20 '17 at 00:32
  • [Find a file](https://stackoverflow.com/questions/4852531/find-files-in-a-folder-using-java) then [Read its contents](https://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file) then print it. –  Nov 20 '17 at 00:38

1 Answers1

0

The code that you show here won't get into the if clause since nameTweets will always be "/Users/john/Documents/workspace/Axis Powers/users", and therefore nameTweets.equalsIgnoreCase(name) will be false.

To get your code working, I would remove your Scanner scan and would use listFiles() instead, in order to get all files in your directory. After that, if the condition verifies, you just have to read the file (e.g. using a BufferedReader) and print the lines out.

Below I provide you a code block that achieves what you want.

FULL CODE

package testsplit;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Fraction {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Who would you want to search for?");
        String name = keyboard.nextLine();
        name += ".txt";

        keyboard.close();

        File dir = new File("/Users/john/Documents/workspace/Axis Powers/users");
        File[] listOfFiles = dir.listFiles();

        String filename, line;

        for(File file : listOfFiles) {
            filename = file.getName();

            if(file.isFile() && filename.equalsIgnoreCase(name) && filename.endsWith(".txt") ){

                try {
                    BufferedReader br = new BufferedReader(new FileReader(dir+"/"+filename));

                    while( (line = br.readLine()) != null) {
                        System.out.println(line);
                    }

                    br.close();

                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
       }
    }
}

Other Notes

  • DO NOT FORGET to close Scanner / BufferedReaders (Streams in general). It is very important, since it can lead to resource leaks during the execution of a program if you don't close them.
LuisFerrolho
  • 417
  • 3
  • 13