Objective-C:Objective-C:文件中一些对目录进行操作的函数

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目录的路径

         NSString *homeDir = NSHomeDirectory();
NSLog(@"home:%@",homeDir);
-- ::28.727 -path[:] home:/Users/mac

//2、获取临时文件夹的路径

         NSString *tempDir = NSTemporaryDirectory();
NSLog(@"home:%@",tempDir);
-- ::28.728 -path[:] home:/var/folders/dx/481d4y4j4430hfty5zr_1gfh0000gn/T/

//3、将路径分成部分

         NSArray *paths = [tempDir pathComponents];
NSLog(@"%@",paths);
-- ::28.728 -path[:] (
"/",
var,
folders,
dx,
"481d4y4j4430hfty5zr_1gfh0000gn",
T,
"/"
)

//4、将部分再拼成路径

         NSString *path = [NSString pathWithComponents:paths];
NSLog(@"%@",path);
-- ::28.729 -path[:] /var/folders/dx/481d4y4j4430hfty5zr_1gfh0000gn/T

//5、在路径上添加文件

         NSString *fileNamePath = [path stringByAppendingPathComponent:@"1.txt"];
NSLog(@"fileName:%@",fileNamePath);
-- ::28.729 -path[:] fileName:/var/folders/dx/481d4y4j4430hfty5zr_1gfh0000gn/T/.txt

//6、获取路径的最后一部分

         NSString *fileName = [fileNamePath lastPathComponent];
NSLog(@"fileName:%@",fileName);
-- ::28.729 -path[:] fileName:.txt

//7、获取文件的扩展名

         NSString *extension = [fileNamePath pathExtension];
NSLog(@"extension:%@",extension);
-- ::28.729 -path[:] extension:txt
Program ended with exit code:
上一篇:[bzoj1023][SHOI2008]cactus 仙人掌图 (动态规划)


下一篇:(转)Android中使用ormlite实现持久化(一)--HelloOrmLite