0

So basically, I'm going to a file location, checking if its .txt. If it is then i read through that file. If it is a directory than I have to recursively and if verbose is true then I also have to output the files as i iterate through them. I am currently trying to list the files. but i keep getting "incompatible types:java.io.File[] cannot be converted to javo.io.File", but i can't think if any other way as i have to pass a file or a directory through the File parameter of collect. I'm not sure i even understand what the question is asking exactly.

Here is the question:

If file is an individual file whose name ends with the extension .txt, the method should read its contents as text one line at the time, passing each line to the previous extractIntegers method. Individual files whose name does not end with .txt should simply be ignored. If file is a directory, the method should recursively call itself for every file and directory inside that one. If the parameter verbose is set to true, the method should also output the name of each file and directory that it processes.

public static void collect(File file, Set<Integer> found, boolean verbose)
{
    String fileName = file.getName(); 
    String extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
    String fileLine = "";
    BufferedReader reader;
    try{
        reader = new BufferedReader(new FileReader(file));
        if(extension.equals("txt"))
        {
            while((fileLine = reader.readLine()) != null)
            {
                extractIntegers(fileLine, found); 
            }
        }
        else if(file.isDirectory()) 
        {
            if(verbose = true)
            {
                System.out.println("Directory: " + file.getName());
                collect(file.listFiles(), found, true);
            }
            else
            {
                collect(file.listFiles(), found, false);
            }
        }   
    }
    catch(IOException e)
    {
        System.out.print("file/directory not found"); 
    }
}
kyle truong
  • 33
  • 1
  • 4
  • I would have always used `SimpleFileVisitor` and `DirectoryStream` for filtering .txt files. Here is an [example](https://docs.oracle.com/javase/tutorial/essential/io/walk.html) – Prashant Apr 11 '16 at 06:20
  • I suppose this is a repeat question. Have a look at http://stackoverflow.com/questions/20987214/recursively-list-all-files-within-a-directory-using-nio-file-directorystream – Allahbaksh Asadullah Apr 11 '16 at 08:19

2 Answers2

0

file.listFiles() returns an array of files. You have to pass them individually to your collect() method:

...
else if(file.isDirectory()) {
  if (verbose)
    System.out.println("Directory: " + file.getName());
  for (File f : file.listFiles())
    collect(f, found, verbose);
}
...
erickson
  • 265,237
  • 58
  • 395
  • 493
0

You are calling collect with an argument file.listFiles(), which returns an array of files. What I assume you want to do is call collect once for each file in your array of files.

Try replacing this:

collect(file.listFiles(), found, true);

with this:

for (File subFile : file.listFiles()) {
 collect(file.listFiles(), found, true);
}

to do that. Make sure to replace the bad code both places it appears in your source code. Let me know if it doesn't work - I will investigate more.

nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • it works syntactically, but for some reason when I'm checking whether or not it is a directory, an IOException is always caught. also thank you for the help – kyle truong Apr 11 '16 at 18:30
  • @kyletruong no problem. If my answer helped you it would be cool if you upvoted or accepted it as an answer. – nhouser9 Apr 12 '16 at 03:01