使用目录的常用方法:
//获取当前目录
- (NSString *)currentDirectoryPath
//更改当前目录
- (BOOL)changeCurrentDirectoryPath:(NSString *)path
//复制目录或文件
- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
//创建一个新的目录
- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error
//测试文件是不是目录
- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory
//列出目录下的内容(不会递归)
- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error
//枚举目录内容(会递归)
- (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path
//删除一个文件,文件夹,链接
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
//移动(重命名)目录到一个指定的路径
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
//操作目录
NSFileManager *fm = [[NSFileManager alloc]init];
NSString *dirName = @"testdir";
NSString *path = [fm currentDirectoryPath]; //获取当前目录路径.
//新建目录
if ([fm createDirectoryAtPath:dirName withIntermediateDirectories:YES attributes:nil error:NULL] == NO) {
NSLog(@"Couldn't create dir");
}
//重命名目录
if ([fm moveItemAtPath:dirName toPath:@"newdir" error:NULL] == NO) {
NSLog(@"Directory rename failed");
}
//修改路径到新目录
if ([fm changeCurrentDirectoryPath:@"newdir"] == NO) {
NSLog(@"change directory failed");
}
//返回当前路径
path = [fm currentDirectoryPath];
NSLog(@"Current directory path is %@",path);
//删除目录
if ([fm removeItemAtPath:path error:NULL] == NO) {
NSLog(@"remove directory failed");
}
//枚举目录中的内容
NSArray *dirArray;
NSFileManager *fm = [[NSFileManager alloc]init];
NSString *dirPath = [fm currentDirectoryPath]; //当前目录
NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath:dirPath]; //开始枚举过程,将其存入dirEnum中.
//向dirEnum发送nextObject消息,返回下一个文件路径,当没有可供枚举时,返回nil.
//enumeratorAtPath:方法会递归打印.
NSString *file;
while ((file = [dirEnum nextObject])) {
if ([[file pathExtension] isEqualToString: @"doc"]){ //找出目录下面所有的doc文件
NSString *fullPath = [dirPath stringByAppendingPathComponent:file];
NSLog(@"%@",fullPath);
}
}
dirArray = [fm contentsOfDirectoryAtPath:[fm currentDirectoryPath] error:NULL];
NSLog(@"内容为:"); //使用contentsOfDirectoryAtPath:方法枚举当前路径中的文件并存入数组dirArray.
for (NSString *path in dirArray){ //快速枚举数组中的内容并打印.
NSLog(@"%@",path);
}
NSFileManager *fm = [[NSFileManager alloc]init];
NSString *fName = @"path.m";
NSString *upath = @"~s0s0/123/xxx/../321/./path.m";
NSString *path, *tempdir, *extension, *homedir, *fullpath;
NSArray *components;
fm = [[NSFileManager alloc]init];
//NSTemporaryDirectory()函数返回可以用来创建临时文件的目录路径名,如果要创建文件,完成任务后要删除;确保文件名是唯一的.
tempdir = NSTemporaryDirectory();
NSLog(@"临时文件夹路径为:%@",tempdir);
path = [fm currentDirectoryPath]; //返回当前目录路径
//lastPathComponent方法用与提取路径中最后一个目录名.
NSLog(@"父目录名: %@",[path lastPathComponent]);
//stringByAppendingPathComponent:方法将文件名插入到路径的末尾,这样就能显示一个文件的完整路径.
fullpath = [path stringByAppendingPathComponent:fName];
NSLog(@"完整路径为:%@ to %@",fName,fullpath);
//pathExtension方法返回一个完整路径中的文件扩展名,如果没有扩展名,就返回空字符.
extension = [fullpath pathExtension];
NSLog(@"extension for %@ is %@ ",fullpath, extension);
//NSHomeDirectory()函数返回当前用户的主目录
//NSHomeDirectoryForUser(username)函数可以提供用户名做参数,并返回主目录名
homedir = NSHomeDirectory();
NSLog(@"主目录为 : %@",homedir);
//pathComponents方法返回一个数组,数组中包含一个路径中的每个组成部分.
components = [homedir pathComponents];
for (path in components){
NSLog(@"%@",path); //依次输出数组components中保存的元素.
}
//stringByStandardizingPath方法将原路径中的代字符转化为完整路径.
//如果是路径名字中出现代字符,可以使用stringByExpandingTildeInPath方法.
NSLog(@"%@ -> %@",upath, [upath stringByStandardizingPath]);