I have to input a directory in DT, then have Directory recursively return a list of files and folders in that directory.
My problem is that at the moment my program is only returning the first file in the directory and then ending.
I know that I'm doing my recursion method wrong, but I can't seem to fix it. Any help would be very helpful.
import java.io.*;
import java.util.*;
public class Directory
{
private String s;
private int i;
public Directory()
{
String s = "";
}
public Directory(String s)
{
this.s = s;
}
public String recursiveMethod(){
File p = new File(s);
String[] filesInDir = p.list();
String k = filesInDir[i];
i++;
if (filesInDir == null) return "";
if (filesInDir != null) return k;
recursiveMethod();
return k;
}
}
Tester
import java.io.*;
import java.util.*;
public class DT
{
public static void main(String args[])
{
System.out.println("Enter directory.");
Scanner scan = new Scanner (System.in);
String s = scan.next();
Directory p = new Directory(s);
printStat(p);
}
private static void printStat(Directory recurse) {
System.out.print(recurse.recursiveMethod() + "\n");
}
}