iOS - 操作文件目录的方法

时间:2023-03-09 00:52:55
iOS - 操作文件目录的方法

转:http://blog.****.net/marujunyy/article/details/11579183

使用目录的常用方法:

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