Skip to content

Instantly share code, notes, and snippets.

@OlgaKulikova
Last active August 29, 2015 14:08
Show Gist options
  • Save OlgaKulikova/d9d6c27b03679a960a3f to your computer and use it in GitHub Desktop.
Save OlgaKulikova/d9d6c27b03679a960a3f to your computer and use it in GitHub Desktop.
Рекурсия. Файлы и каталоги.
package Recursion;
// Написать рекурсивную ф-ю для вывода на экран всех файлов и каталогов,
// имя которых длиннее 5 символов и вторая буква равна ‘A’.
import java.io.File;
import java.util.ArrayList;
public class Main {
private static void listAll(String path, ArrayList<String> res)
throws Exception
{
File dir = new File(path);
File[] list = dir.listFiles();
for (File f : list) {
try {
char a = f.getName().charAt(1); // исправленная строка
if (f.getName().length() > 5 && a == 'a') { // исправленная строка
if (f.isFile()) {
res.add("F: " + f.getCanonicalPath());
} else {
res.add("D: " + f.getCanonicalPath());
listAll(f.getCanonicalPath(), res);
}
}
} catch (NullPointerException | IndexOutOfBoundsException e) {
continue;
}
}
}
public static void main(String[] args) {
final String path = "D:\\Dir";
ArrayList<String> res = new ArrayList<String>();
try {
listAll(path, res);
} catch (Exception e) {
e.printStackTrace();
}
for (String s : res)
System.out.println(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment