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");
}
}