激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java實現遞歸文件列表的方法

java實現遞歸文件列表的方法

2019-12-27 13:10華宰 JAVA教程

這篇文章主要介紹了java實現遞歸文件列表的方法,實例分析了java采用遞歸算法遍歷文件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了java實現遞歸文件列表的方法。分享給大家供大家參考。具體如下:

FileListing.java如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.*;
import java.io.*;
/**
* Recursive file listing under a specified directory.
*
* @author javapractices.com
* @author Alex Wong
* @author anonymous user
*/
public final class FileListing {
 /**
 * Demonstrate use.
 *
 * @param aArgs - <tt>aArgs[0]</tt> is the full name of an existing
 * directory that can be read.
 */
 public static void main(String... aArgs) throws FileNotFoundException {
  File startingDirectory= new File(aArgs[0]);
  List<File> files = FileListing.getFileListing(startingDirectory);
  //print out all file names, in the the order of File.compareTo()
  for(File file : files ){
   System.out.println(file);
  }
 }
 /**
 * Recursively walk a directory tree and return a List of all
 * Files found; the List is sorted using File.compareTo().
 *
 * @param aStartingDir is a valid directory, which can be read.
 */
 static public List<File> getFileListing(
  File aStartingDir
 ) throws FileNotFoundException {
  validateDirectory(aStartingDir);
  List<File> result = getFileListingNoSort(aStartingDir);
  Collections.sort(result);
  return result;
 }
 // PRIVATE //
 static private List<File> getFileListingNoSort(
  File aStartingDir
 ) throws FileNotFoundException {
  List<File> result = new ArrayList<File>();
  File[] filesAndDirs = aStartingDir.listFiles();
  List<File> filesDirs = Arrays.asList(filesAndDirs);
  for(File file : filesDirs) {
   result.add(file); //always add, even if directory
   if ( ! file.isFile() ) {
    //must be a directory
    //recursive call!
    List<File> deeperList = getFileListingNoSort(file);
    result.addAll(deeperList);
   }
  }
  return result;
 }
 /**
 * Directory is valid if it exists, does not represent a file, and can be read.
 */
 static private void validateDirectory (
  File aDirectory
 ) throws FileNotFoundException {
  if (aDirectory == null) {
   throw new IllegalArgumentException("Directory should not be null.");
  }
  if (!aDirectory.exists()) {
   throw new FileNotFoundException("Directory does not exist: " + aDirectory);
  }
  if (!aDirectory.isDirectory()) {
   throw new IllegalArgumentException("Is not a directory: " + aDirectory);
  }
  if (!aDirectory.canRead()) {
   throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
  }
 }
}

希望本文所述對大家的java程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 55夜色66夜色国产精品视频 | 少妇一级淫片高潮流水电影 | 越南一级黄色片 | 欧洲a级片 | xxxxhd18hd日本hd| 美女黄污视频 | 日本成年网 | 日本中文视频 | 男女羞羞的视频 | 本色视频aaaaaa一级网站 | 黄色a级片视频 | www.av88 | 丰满年轻岳中文字幕一区二区 | 亚洲精品一区国产精品丝瓜 | 自拍偷拍亚洲图片 | 久色伊人 | 丁香天堂网 | 精品一区二区久久久久久按摩 | 双性帝王调教跪撅打屁股 | 久草中文网 | 国产88久久久国产精品免费二区 | 日本羞羞影院 | 亚洲av毛片在线观看 | 久久久日韩精品一区二区 | 8x成人在线电影 | 性欧美性欧美 | 99成人在线 | 日本a大片 | 国产精品成年片在线观看, 日韩毛片网 | 亚洲午夜电影 | 国产免费传媒av片在线 | 欧美毛片在线观看 | 国产免费一区二区三区视频 | 免费a观看| 欧美精品一区二区三区四区 | 97久色 | 国产精品一二三区 | 天天看夜夜爽 | 依依成人综合 | 懂色av懂色aⅴ精彩av | 国产精品999在线 |