Created
January 30, 2016 10:06
-
-
Save Viacheslav77/cc1df89e386395565acc to your computer and use it in GitHub Desktop.
Написать рекурсивную ф-ю для вывода на экран всех файлов и каталогов, имя которых длиннее 5 символов и вторая буква равна ‘A’.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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