• iOS


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

    使用目录的常用方法:

    1. //获取当前目录  
    2. - (NSString *)currentDirectoryPath   
    3.   
    4. //更改当前目录  
    5. - (BOOL)changeCurrentDirectoryPath:(NSString *)path    
    6.   
    7. //复制目录或文件  
    8. - (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error    
    9.   
    10. //创建一个新的目录  
    11. - (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error    
    12.   
    13. //测试文件是不是目录  
    14. - (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory  
    15.   
    16. //列出目录下的内容(不会递归)  
    17. - (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error  
    18.   
    19. //枚举目录内容(会递归)  
    20. - (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path  
    21.   
    22. //删除一个文件,文件夹,链接  
    23. - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error  
    24.   
    25. //移动(重命名)目录到一个指定的路径  
    26. - (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. //新建目录  
    7. if ([fm createDirectoryAtPath:dirName withIntermediateDirectories:YES attributes:nil error:NULL] == NO) {  
    8.     NSLog(@"Couldn't create dir");   
    9. }  
    10.   
    11. //重命名目录   
    12. if ([fm moveItemAtPath:dirName toPath:@"newdir" error:NULL] == NO) {  
    13.     NSLog(@"Directory rename failed");   
    14. }  
    15.   
    16. //修改路径到新目录  
    17. if ([fm changeCurrentDirectoryPath:@"newdir"] == NO) {  
    18.     NSLog(@"change directory failed");   
    19. }  
    20.   
    21. //返回当前路径  
    22. path = [fm currentDirectoryPath];   
    23. NSLog(@"Current directory path is %@",path);  
    24.   
    25. //删除目录  
    26. if ([fm removeItemAtPath:path error:NULL] == NO) {  
    27.     NSLog(@"remove directory failed");  
    28. }  
    1. //枚举目录中的内容  
    2. NSArray *dirArray;  
    3. NSFileManager *fm = [[NSFileManager alloc]init];  
    4. NSString *dirPath = [fm currentDirectoryPath];  //当前目录  
    5. NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath:dirPath]; //开始枚举过程,将其存入dirEnum中.  
    6.   
    7. //向dirEnum发送nextObject消息,返回下一个文件路径,当没有可供枚举时,返回nil.  
    8. //enumeratorAtPath:方法会递归打印.  
    9. NSString *file;  
    10. while ((file = [dirEnum nextObject])) {  
    11.     if ([[file pathExtension] isEqualToString: @"doc"]){ //找出目录下面所有的doc文件  
    12.         NSString *fullPath = [dirPath stringByAppendingPathComponent:file];  
    13.         NSLog(@"%@",fullPath);  
    14.     }   
    15. }  
    16.   
    17. dirArray = [fm contentsOfDirectoryAtPath:[fm currentDirectoryPath] error:NULL];  
    18. NSLog(@"内容为:"); //使用contentsOfDirectoryAtPath:方法枚举当前路径中的文件并存入数组dirArray.    
    19. for (NSString *path in dirArray){  //快速枚举数组中的内容并打印.  
    20.     NSLog(@"%@",path);  
    21. }  


    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.   
    7. fm = [[NSFileManager alloc]init];  
    8.   
    9. //NSTemporaryDirectory()函数返回可以用来创建临时文件的目录路径名,如果要创建文件,完成任务后要删除;确保文件名是唯一的.  
    10. tempdir = NSTemporaryDirectory();    
    11. NSLog(@"临时文件夹路径为:%@",tempdir);  
    12.   
    13. path = [fm currentDirectoryPath]; //返回当前目录路径  
    14. //lastPathComponent方法用与提取路径中最后一个目录名.  
    15. NSLog(@"父目录名: %@",[path lastPathComponent]);  
    16.   
    17. //stringByAppendingPathComponent:方法将文件名插入到路径的末尾,这样就能显示一个文件的完整路径.  
    18. fullpath = [path stringByAppendingPathComponent:fName];   
    19. NSLog(@"完整路径为:%@ to %@",fName,fullpath);   
    20.   
    21. //pathExtension方法返回一个完整路径中的文件扩展名,如果没有扩展名,就返回空字符.  
    22. extension = [fullpath pathExtension];   
    23. NSLog(@"extension for %@ is %@ ",fullpath, extension);  
    24.   
    25. //NSHomeDirectory()函数返回当前用户的主目录  
    26. //NSHomeDirectoryForUser(username)函数可以提供用户名做参数,并返回主目录名  
    27. homedir = NSHomeDirectory();   
    28. NSLog(@"主目录为 : %@",homedir);  
    29.   
    30. //pathComponents方法返回一个数组,数组中包含一个路径中的每个组成部分.  
    31. components = [homedir pathComponents];   
    32. for (path in components){  
    33.     NSLog(@"%@",path); //依次输出数组components中保存的元素.  
    34. }  
    35.       
    36. //stringByStandardizingPath方法将原路径中的代字符转化为完整路径.  
    37. //如果是路径名字中出现代字符,可以使用stringByExpandingTildeInPath方法.   
    38. NSLog(@"%@ -> %@",upath, [upath stringByStandardizingPath]); 

  • 相关阅读:
    Matlab下imwrite,Uint16的深度图像
    ROS indigo下Kinect v1的驱动安装与调试
    ROS indigo下Kinect v2的驱动安装与调试
    Ubuntu14.04(indigo)实现RGBDSLAMv2(数据集和实时Kinect)
    Ubuntu验证查看库的安装情况-copied
    Ubuntu常用命令及git常用命令-copied
    针孔相机模型和相机镜头畸变模型
    Ubuntu14.04-OpenCV2和3共存相关设置
    Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤
    window和Linux下Redis的安装及运行
  • 原文地址:https://www.cnblogs.com/jackljf/p/4768600.html
Copyright © 2020-2023  润新知