Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Created January 30, 2016 10:06
Show Gist options
  • Select an option

  • Save Viacheslav77/cc1df89e386395565acc to your computer and use it in GitHub Desktop.

Select an option

Save Viacheslav77/cc1df89e386395565acc to your computer and use it in GitHub Desktop.
Написать рекурсивную ф-ю для вывода на экран всех файлов и каталогов, имя которых длиннее 5 символов и вторая буква равна ‘A’.
package Recursion;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/*Написать рекурсивную ф-ю для вывода на экран всех файлов и каталогов, имя которых длиннее 5
символов и вторая буква равна ‘A’.*/
public class Main {
private static void searchTerms (String s, ArrayList<String> res, File f ) throws IOException{
if (f.getName().length()>1 && f.getName().length()<=5 && f.getName().charAt(1)==65){
System.out.println(f.getName().charAt(1));
//if(f.getName().charAt(1)==65)
res.add(s + f.getCanonicalPath());
//else return;
}
}
private static void listAll(String path, ArrayList<String> res)
throws IOException
{
File dir = new File(path);
File[] list = dir.listFiles();
for (File f : list) {
if (f.isFile()) {
if ( f.getName().length()>1 && f.getName().length()<=8 && f.getName().charAt(1) == (char) 65)
res.add(" File : " + f.getCanonicalPath());
} else {
if ( f.getName().length()>1 && f.getName().length()<=8 && f.getName().charAt(1) == (char) 65)
res.add("Dir : " + f.getCanonicalPath());
listAll(f.getCanonicalPath(), res);
}
}
}
public static void main(String[] args) {
final String path = "d:\\1";
ArrayList<String> res = new ArrayList<String>();
try {
listAll(path, res);
} catch (IOException 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