및 루트 디렉토리 아래의 각 파일의 디렉토리 아래에 각 파일의 디렉토리는 재귀 적으로디스플레이 모든 파일과 루트 폴더 재귀
출력이
과 같아야합니다파일 이름 ---> 해당 파일이 들어있는 디렉토리는
예를 들어 filename.jpg는 ---> C : \ 작업 공간은 filename.jpg가에
C : 즉 작업 공간 \ 경로는 c : \ workspace \ filename.txt 거기 ar 각 디렉토리
및 루트 디렉토리 아래의 각 파일의 디렉토리 아래에 각 파일의 디렉토리는 재귀 적으로디스플레이 모든 파일과 루트 폴더 재귀
출력이
과 같아야합니다파일 이름 ---> 해당 파일이 들어있는 디렉토리는
예를 들어 filename.jpg는 ---> C : \ 작업 공간은 filename.jpg가에
C : 즉 작업 공간 \ 경로는 c : \ workspace \ filename.txt 거기 ar 각 디렉토리
같은 이름의 파일 이름이 솔루션에서 재정의 될 것으로 기억에서 많은 파일을 이메일 (이 허용하는 Map<String, List<File>>
필요) :
public static void main(String[] args) throws Exception {
Map<String, File> map = getFiles(new File("."));
for (String name : map.keySet())
if (name.endsWith(".txt")) // display filter
System.out.println(name + " ---> " + map.get(name));
}
private static Map<String, File> getFiles(File current) {
Map<String, File> map = new HashMap<String, File>();
if (current.isDirectory()) {
for (File file : current.listFiles()) {
map.put(file.getName(), current);
map.putAll(getFiles(file));
}
}
return map;
}
예 출력 :
test1.txt ---> .
test2.txt ---> .\doc
test3.txt ---> .\doc\test
을 Apache Commons Fileutils을 사용할 수 있습니다.
public static void main(String[] args) throws IOException {
File rootDir = new File("/home/marco/tmp/");
Collection<File> files = FileUtils.listFiles(rootDir, new String[] {
"jpeg", "log" }, true);
for (File file : files) {
String path = file.getAbsolutePath();
System.out.println(file.getName() + " -> "
+ path.substring(0, path.lastIndexOf('/')));
}
}
첫 번째 인수 listFiles
은 검색을 시작하려는 디렉토리이고 두 번째 인수는 원하는 파일 확장명을 제공하는 String
의 배열이고 세 번째 인수는 검색이 재귀 적인지 여부를 나타내는 boolean
입니다.
예 출력 :
visicheck.jpeg -> /home/marco/tmp
connettore.jpeg -> /home/marco/tmp
sme2.log -> /home/marco/tmp/sme2_v2_1/log
davmail_smtp.jpeg -> /home/marco/tmp
당신은 무엇을 시도? java.io.File 클래스에는 이것을 구현하는 데 필요한 모든 것이있다. http://download.oracle.com/javase/6/docs/api/java/io/File.html –
첫 번째 Google hit ['Recursive File Listing'] (http://www.javapractices.com/topic/) TopicAction.do?Id=68) – JRL