IO文件中,一些对目录进行操作的函数:获取、切分、组合
一些对目录进行操作的函数:
获取用户的姓名:(NSString*)NSUserName() ; ———>NSString *Str = NSUserName()
获取home目录路径:(NSString*)NSHomeDirectory()
获取临时文件夹路径:(NSString*)NSTemporaryDirectory()
一些对目录进行操作的方法:
将路径切成部分:- (NSArray *)pathComponents ———>NSArray *paths = [path pathComponents]
将部分组合成路径:+ (NSString *)pathWithComponents:(NSArray *)components
判断是不是绝对路径:- (BOOL)isAbsolutePath
获取路径最后一部分:- (NSString *)lastPathComponent
删除最后一部分:- (NSString *)stringByDeletingLastPathComponent
获取扩展名:- (NSString *)pathExtension
删除扩展名:- (NSString *)stringByDeletingPathExtension
添加路径:- (NSString *)stringByAppendingPathComponent:(NSString *)str
添加扩展名:- (NSString *)stringByAppendingPathExtension:(NSString *)str
//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
程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4755962.html,如需转载请自行联系原作者