有些时候,你 会需要将 目录的内容 列举出来。要将 目录的内容列举出来,既可以采取 enumeratorAtPath:这项措施 也可以采取 directoryContentsAtPath:这项措施。如果 采取 enumeratorAtPath:这项措施,那么 子目录中的内容 也会被列举出来。
如果 采取 directoryContentsAtPath:这项措施,那么 特定目录中的内容 就会被列举出来,列举的结果 会存储 在数组物件当中。如果 目录当中 包含 子目录,那么 子目录的内容 则不会被列举出来。
下面的程序 示范了 如何列举 目录中的内容:
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 | #import <Foundation/NSArray.h> #import <Foundation/NSString.h> #import <Foundation/NSFileManager.h> #import <Foundation/NSAutoreleasePool.h> int main(int argc,const char *argv[]) { NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init]; NSString *path=@"/Users/Zijin/Desktop/Sample"; NSFileManager *myFileManager=[NSFileManager defaultManager]; NSDirectoryEnumerator *myDirectoryEnumerator; NSArray *directoryContents; myDirectoryEnumerator=[myFileManager enumeratorAtPath:path]; //列举目录内容 NSLog(@"用enumeratorAtPath:显示目录%@的内容:",path); while((path=[myDirectoryEnumerator nextObject])!=nil) { NSLog(@"%@",path); } //用另外一种办法列举目录内容 directoryContents=[myFileManager directoryContentsAtPath:@"/Users/Zijin/Desktop/Sample"]; NSLog(@"用directoryContentsAtPath:显示目录%@的内容:",@"/Users/Zijin/Desktop/Sample"); for(path in directoryContents) { NSLog(@"%@",path); } [pool drain]; return 0; } |
运行 这个程序后,可以得到 这样的结果
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
77
78
79
80
81
82
83
84
|
2012-05-13 17:30:46.379 Sample[3665:707] 用enumeratorAtPath:显示目录/Users/Zijin/Desktop/Sample的内容:
2012-05-13 17:30:46.380 Sample[3665:707] .DS_Store
2012-05-13 17:30:46.381 Sample[3665:707] .git
2012-05-13 17:30:46.381 Sample[3665:707] .git/branches
2012-05-13 17:30:46.382 Sample[3665:707] .git/COMMIT_EDITMSG
2012-05-13 17:30:46.382 Sample[3665:707] .git/config
2012-05-13 17:30:46.383 Sample[3665:707] .git/description
2012-05-13 17:30:46.383 Sample[3665:707] .git/HEAD
2012-05-13 17:30:46.384 Sample[3665:707] .git/hooks
2012-05-13 17:30:46.384 Sample[3665:707] .git/hooks/applypatch-msg.sample
2012-05-13 17:30:46.385 Sample[3665:707] .git/hooks/commit-msg.sample
2012-05-13 17:30:46.385 Sample[3665:707] .git/hooks/post-commit.sample
2012-05-13 17:30:46.386 Sample[3665:707] .git/hooks/post-receive.sample
2012-05-13 17:30:46.386 Sample[3665:707] .git/hooks/post-update.sample
2012-05-13 17:30:46.387 Sample[3665:707] .git/hooks/pre-applypatch.sample
2012-05-13 17:30:46.387 Sample[3665:707] .git/hooks/pre-commit.sample
2012-05-13 17:30:46.387 Sample[3665:707] .git/hooks/pre-rebase.sample
2012-05-13 17:30:46.388 Sample[3665:707] .git/hooks/prepare-commit-msg.sample
2012-05-13 17:30:46.388 Sample[3665:707] .git/hooks/update.sample
2012-05-13 17:30:46.389 Sample[3665:707] .git/index
2012-05-13 17:30:46.389 Sample[3665:707] .git/info
2012-05-13 17:30:46.390 Sample[3665:707] .git/info/exclude
2012-05-13 17:30:46.390 Sample[3665:707] .git/logs
2012-05-13 17:30:46.391 Sample[3665:707] .git/logs/HEAD
2012-05-13 17:30:46.391 Sample[3665:707] .git/logs/refs
2012-05-13 17:30:46.392 Sample[3665:707] .git/logs/refs/heads
2012-05-13 17:30:46.392 Sample[3665:707] .git/logs/refs/heads/master
2012-05-13 17:30:46.393 Sample[3665:707] .git/objects
2012-05-13 17:30:46.393 Sample[3665:707] .git/objects/08
2012-05-13 17:30:46.394 Sample[3665:707] .git/objects/08/0078c5decbfcb73e9665030b6221e1ccb693be
2012-05-13 17:30:46.394 Sample[3665:707] .git/objects/0d
2012-05-13 17:30:46.395 Sample[3665:707] .git/objects/0d/96feb3eb602c24f8e068f6fd88f569e4e58093
2012-05-13 17:30:46.416 Sample[3665:707] .git/objects/48
2012-05-13 17:30:46.418 Sample[3665:707] .git/objects/48/ef81ae71287847f926dbb9ccca11f8c873a7dc
2012-05-13 17:30:46.419 Sample[3665:707] .git/objects/8c
2012-05-13 17:30:46.420 Sample[3665:707] .git/objects/8c/ce74a0c6d3b68a40659921f9103f9d214088d0
2012-05-13 17:30:46.431 Sample[3665:707] .git/objects/9d
2012-05-13 17:30:46.431 Sample[3665:707] .git/objects/9d/955cc57da917155851b38806a7111120ab70da
2012-05-13 17:30:46.432 Sample[3665:707] .git/objects/c2
2012-05-13 17:30:46.432 Sample[3665:707] .git/objects/c2/7b00431338e78a7d560f021aac076f62e71ba4
2012-05-13 17:30:46.433 Sample[3665:707] .git/objects/ca
2012-05-13 17:30:46.434 Sample[3665:707] .git/objects/ca/c4dcdd18b32e1544ca2e8ef98dbc64d472c171
2012-05-13 17:30:46.434 Sample[3665:707] .git/objects/da
2012-05-13 17:30:46.435 Sample[3665:707] .git/objects/da/2c288712be3bedabfebb188547b14bac334f52
2012-05-13 17:30:46.435 Sample[3665:707] .git/objects/db
2012-05-13 17:30:46.436 Sample[3665:707] .git/objects/db/e1edcebd33105840f71feff89af8c65a0ca527
2012-05-13 17:30:46.436 Sample[3665:707] .git/objects/e4
2012-05-13 17:30:46.437 Sample[3665:707] .git/objects/e4/4c0a7d31a66cdd89e076ffceda1121d5b23440
2012-05-13 17:30:46.437 Sample[3665:707] .git/objects/e5
2012-05-13 17:30:46.438 Sample[3665:707] .git/objects/e5/09e6dc045e6f65e373e54a8142ff3395af93be
2012-05-13 17:30:46.438 Sample[3665:707] .git/objects/e5/e411b533ba3df7f530c4d5f264d87266ebdb93
2012-05-13 17:30:46.510 Sample[3665:707] .git/objects/e6
2012-05-13 17:30:46.512 Sample[3665:707] .git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391
2012-05-13 17:30:46.513 Sample[3665:707] .git/objects/info
2012-05-13 17:30:46.513 Sample[3665:707] .git/objects/pack
2012-05-13 17:30:46.513 Sample[3665:707] .git/refs
2012-05-13 17:30:46.514 Sample[3665:707] .git/refs/heads
2012-05-13 17:30:46.514 Sample[3665:707] .git/refs/heads/master
2012-05-13 17:30:46.515 Sample[3665:707] .git/refs/tags
2012-05-13 17:30:46.515 Sample[3665:707] Sample
2012-05-13 17:30:46.516 Sample[3665:707] Sample/.DS_Store
2012-05-13 17:30:46.516 Sample[3665:707] Sample/main.m
2012-05-13 17:30:46.517 Sample[3665:707] Sample/Sample-Prefix.pch
2012-05-13 17:30:46.517 Sample[3665:707] Sample/Sample.1
2012-05-13 17:30:46.518 Sample[3665:707] Sample.xcodeproj
2012-05-13 17:30:46.518 Sample[3665:707] Sample.xcodeproj/project.pbxproj
2012-05-13 17:30:46.519 Sample[3665:707] Sample.xcodeproj/project.xcworkspace
2012-05-13 17:30:46.519 Sample[3665:707] Sample.xcodeproj/project.xcworkspace/contents.xcworkspacedata
2012-05-13 17:30:46.520 Sample[3665:707] Sample.xcodeproj/project.xcworkspace/xcuserdata
2012-05-13 17:30:46.520 Sample[3665:707] Sample.xcodeproj/project.xcworkspace/xcuserdata/Zijin.xcuserdatad
2012-05-13 17:30:46.521 Sample[3665:707] Sample.xcodeproj/project.xcworkspace/xcuserdata/Zijin.xcuserdatad/UserInterfaceState.xcuserstate
2012-05-13 17:30:46.521 Sample[3665:707] Sample.xcodeproj/xcuserdata
2012-05-13 17:30:46.522 Sample[3665:707] Sample.xcodeproj/xcuserdata/Zijin.xcuserdatad
2012-05-13 17:30:46.525 Sample[3665:707] Sample.xcodeproj/xcuserdata/Zijin.xcuserdatad/xcdebugger
2012-05-13 17:30:46.526 Sample[3665:707] Sample.xcodeproj/xcuserdata/Zijin.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
2012-05-13 17:30:46.527 Sample[3665:707] Sample.xcodeproj/xcuserdata/Zijin.xcuserdatad/xcschemes
2012-05-13 17:30:46.527 Sample[3665:707] Sample.xcodeproj/xcuserdata/Zijin.xcuserdatad/xcschemes/Sample.xcscheme
2012-05-13 17:30:46.528 Sample[3665:707] Sample.xcodeproj/xcuserdata/Zijin.xcuserdatad/xcschemes/xcschememanagement.plist
2012-05-13 17:30:46.528 Sample[3665:707] 用directoryContentsAtPath:显示目录/Users/Zijin/Desktop/Sample的内容:
2012-05-13 17:30:46.529 Sample[3665:707] .DS_Store
2012-05-13 17:30:46.529 Sample[3665:707] .git
2012-05-13 17:30:46.529 Sample[3665:707] Sample
2012-05-13 17:30:46.530 Sample[3665:707] Sample.xcodeproj
Program ended with exit code: 0
|
仔细 看看 这几行语句:
1 2 3 4 5 6 7 8 9 10 11 12 13 | myDirectoryEnumerator=[myFileManager enumeratorAtPath:path]; //列举目录内容 NSLog(@"用enumeratorAtPath:显示目录%@的内容:",path); while((path=[myDirectoryEnumerator nextObject])!=nil) { NSLog(@"%@",path); } |
首先 对 文件管理器物件myFileManager 采取了 enumeratorAtPath:这项措施。enumeratorAtPath:这项措施 会将 一个NSDictionaryEnumerator物件 存储 在myDictionaryEnumerator当中。
在
1
|
while((path=[myDirectoryEnumeratornextObject])!=nil)
|
这行语句中 每当你 对 myDictionaryEnumerator这个物件 采取 nextObject这项措施,你 都会得到 目录中下一个文件或者目录的路径,并且 存储 在变量path当中,再 利用 NSLog()函数 显示出来。直到 所有文件或者目录的路径都 找到后,你 就会得到 一个空值nil,并且 跳出 while()循环。
在程序的运行结果当中 你 可以看到 两种列举目录内容的技巧之不同。enumeratorAtPath:会将 目录中子目录的内容 列举出来,而 directoryContentsAtPath: 则不会。在while()循环执行的过程中 我们 可以这样避免 子目录中的内容 被列举出来:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
BOOLflag;
while((path=[myDirectoryEnumeratornextObject])!=nil)
{
NSLog(@"%@",path);
[myFileManagerfileExistsAtPath:pathisDirectory:&flag];
if(flag==YES)
{
[myDirectoryEnumeratorskipDescendents];
}
}
|
其中 flag 是 BOOL类型的变量。如果 当前的路径path 是 目录,那么 fileExistsAtPath:isDirectory:这项措施 就会将 YES 存储 在flag当中;如果 不是,则会将 NO 存储 在flag当中。