Objective-C:Objective-C:文件中一些对目录进行操作的函数
IO文件中,一些对目录进行操作的函数:获取、切分、组合
//1、获取home目录的路径
1 NSString *homeDir = NSHomeDirectory();
2 NSLog(@"home:%@",homeDir);
2015-08-24 21:17:28.727 05-path[992:45425] home:/Users/mac
//2、获取临时文件夹的路径
1 NSString *tempDir = NSTemporaryDirectory();
2 NSLog(@"home:%@",tempDir);
2015-08-24 21:17:28.728 05-path[992:45425] home:/var/folders/dx/481d4y4j4430hfty5zr_1gfh0000gn/T/
//3、将路径分成部分
1 NSArray *paths = [tempDir pathComponents];
2 NSLog(@"%@",paths);
2015-08-24 21:17:28.728 05-path[992:45425] (
"/",
var,
folders,
dx,
"481d4y4j4430hfty5zr_1gfh0000gn",
T,
"/"
)
//4、将部分再拼成路径
1 NSString *path = [NSString pathWithComponents:paths];
2 NSLog(@"%@",path);
2015-08-24 21:17:28.729 05-path[992:45425] /var/folders/dx/481d4y4j4430hfty5zr_1gfh0000gn/T
//5、在路径上添加文件
1 NSString *fileNamePath = [path stringByAppendingPathComponent:@"1.txt"];
2 NSLog(@"fileName:%@",fileNamePath);
2015-08-24 21:17:28.729 05-path[992:45425] fileName:/var/folders/dx/481d4y4j4430hfty5zr_1gfh0000gn/T/1.txt
//6、获取路径的最后一部分
1 NSString *fileName = [fileNamePath lastPathComponent];
2 NSLog(@"fileName:%@",fileName);
2015-08-24 21:17:28.729 05-path[992:45425] fileName:1.txt
//7、获取文件的扩展名
1 NSString *extension = [fileNamePath pathExtension];
2 NSLog(@"extension:%@",extension);
2015-08-24 21:17:28.729 05-path[992:45425] extension:txt
Program ended with exit code: 0
