d :/a 디렉토리의 구조가 다음과 같습니다.
d :/a | --a.sql | ---back.log | ---- | | --e | | -1.txt | | -2.txt | | -3.txt | `--f | | -4.txt | | -5.txt | `--6.txt |--c | | --e | | -ace1.txt | | -ace2.txt | | -ace3.txt | `--f | | -4.txt | | -5.txt | `--6.txt`-d | --a.java | --Abc (1) .txt | --Abc (2) .txt | --Abc (3) .txt | ---b.java` ---c.java
4.1 예 1 : 전체 디렉토리의 파일 목록 (재귀)
아이디어 :
1. 디렉토리 d :/a를 전송합니다.
2. d :/a의 디렉토리로 이동할 때 마다이 하위 디렉토리를 반복하십시오. 따라서 트래버 된 각 요소가 디렉토리인지 여부를 결정해야합니다.
다음은 일반 코드에서 재귀 코드로의 코드 중 일부입니다.
file dir = 새 파일 ( "d :/a"); file [] file_list = dir.listfiles (); for (file list : file_list) {if (list.isdirectory ()) {file dir_1 = list.listfiles (); // 코드가 여기에서 시작하여 (dir_1.isdirectory ()) {....}} else {system.out.println (list.getabsolutepath ()); }}반복 된 코드 부품이 캡슐화되므로 재귀 방법은 코드를 캡슐화하고 무한 재귀 문제를 해결하는 데 사용됩니다. 최종 코드는 다음과 같습니다.
import java.io.*; public class listallfiles {public static void main (String [] args) {file dir = new File ( "d :/a"); System.out.println ( "dir ------>"+dir.getabsolutepath ()); ListAll (dir); } public static void listall (file dir) {file [] file_list = dir.listfiles (); for (file : file_list) {if (file.isdirectory ()) {system.out.println ( "dir ------>"+file.getabsolutepath ()); ListAll (파일); } else {system.out.println ( "file ------>"+file.getabsolutepath ()); }}}}4.2 예제 2 : 전체 디렉토리의 파일 목록 (큐)
아이디어 :
1. 주어진 디렉토리를 반복하십시오. 컬렉션에 통과 한 디렉토리 이름을 넣으십시오.
2. 컬렉션의 각 디렉토리 요소를 반복하고 컬렉션에 트래버스 된 하위 디렉토리를 추가하십시오. 마지막으로, 각 트래버스 후 컬렉션에서 삭제하십시오.
3. 이런 식으로 디렉토리가 발견되는 한 특정 디렉토리가 횡단 될 때까지 계속 가로 지르고 같은 수준의 다음 디렉토리가 횡단되기 시작합니다.
고려해야 할 것은 어떤 종류의 컬렉션을 사용할 수 있습니다. 우선, 컬렉션의 디렉토리 요소를 정렬 할 필요가 없으며 다른 디렉토리의 하위 디렉토리 이름을 반복 할 수 있습니다. 따라서 세트 컬렉션 대신 목록 컬렉션이 사용됩니다. 요소가 자주 추가되고 삭제되므로 ArrayList 컬렉션 대신 링크드리스트가 사용됩니다. LinkedList 컬렉션의 가장 두드러진 기능은 FIFO 대기열입니다.
재귀 트래버스와 비교할 때, 디렉토리를 통과하기 위해 큐를 사용하는 이점은 요소가 컨테이너에 배치되며 모두 힙 메모리에 있으므로 메모리를 넘어서는 것이 어렵다는 것입니다.
import java.util.*; import java.io.*; public class listallfiles2 {public static void main (String [] args) {file dir = new File ( "d :/a"); 큐 <파일> file_queue = new queue <file> (); // 큐 파일을 구성합니다 [] list = dir.listfiles (); for (file file : list) {// 최상위 디렉토리 If (file.isdirectory ()) {system.out.println ( "dir ----->"+file.getabsolutepath ()); file_queue.add (파일); } else {system.out.println ( "file ------>"+file.getabsolutepath ()); }} while (! file_queue.isnull ()) {// secondary subdirectory에서 시작, 트래버스 파일 subdirs = file_queue.get (); // 먼저 보조 하위 디렉토리 이름 파일을 가져옵니다 [] subfiles = subdirs.listfiles (); for (file subdir : subfiles) {// 각각의 다음 단계 하위 디렉토리 if (subdir.isdirectory ()) {system.out.println ( "dir ------>"+subdir.getabsolutepath ()); file_queue.add (subdir); // 내부 계층에 서브 디렉토리가있는 경우 큐에 추가} else {System.out.println ( "file ------>"+subdir.getabsolutepath ()); }}}}}}}}}} 클래스 큐 <e> {private linkedList <E> LinkedList; queue () {linkedList = new LinkedList <e> (); } public void add (e e) {linkedList.AddFirst (e); // advanced} public e get () {return linkedList.removelast (); // First Out} public boolean isnull () {return linkedList.isempty (); }}4.3 예 3 : 트리 구조 전체 디렉토리에 파일이 표시됩니다 (재귀)
아이디어 :
1. 먼저 첫 번째 수준 디렉토리 및 파일을 나열하십시오.
2. 디렉토리 인 경우 트리 모양을 형성하는 접두사 기호를 추가하십시오. 그런 다음이 디렉토리를 통해 반복하여 재귀적인 트래버스가 필요합니다.
import java.io.*; public class treefiles {public static void main (String [] args) {file dir = new File ( "d :/a"); System.out.println (dir.getName ()); ListChilds (Dir, 1); } public static void listchilds (파일 f, int level) {문자열 prefix = ""; for (int i = 0; i <level; i ++) {prefix = "|"+prefix; } file [] files = f.listfiles (); for (파일 : 파일) {if (file.isdirectory ()) {System.out.println (prefix + file.getName ()); ListChilds (파일, 레벨+1); } else {system.out.println (prefix + file.getName ()); }}}}결과는 다음과 같습니다.
A | a.sql | B | | e | | 1.txt | | | 2.txt | | | 3.txt | | f | | 4.txt | | | 5.txt | | | 6.txt | 뒤로 .Log | C | | e | | | Ace1.txt | | | Ace2.txt | | | ace3.txt | | f | | | 4.txt | | | 5.txt | | | 6.txt | d | | a.java | | ABC (1) .txt | | ABC (2) .txt | | ABC (3) .txt | | B. 자바 | | C. 자바
4.4 전체 디렉토리를 삭제합니다
import java.io.*; public class filedelete {public static void main (String [] args) {file file = new File ( "d :/a"); rm (파일); } public static void rm (file f) {if (! f.exists ()) {system.out.println ( "파일 찾기!"); 반품; } else if (f.isfile ()) {f.delete (); 반품; } file [] dir = f.listfiles (); for (파일 파일 : dir) {rm (파일); } f.delete (); }}요약
위의 것은 Java Display Directory 파일 목록이며 편집기가 소개 한 DEFLETORY입니다. 모든 사람에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 모든 사람에게 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!