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

    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
  • 相关阅读:
    lua的多种实现方式(1-100的和)
    51单片机交通灯(定时器+38译码器+中断)
    51单片机定时器实现LED闪烁
    51单片机0号与1号外部中断实例
    51单片机:IO口扩展芯片用法(74HC165,74HC595)
    mybatis org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
    Intellij IDEA运行报Command line is too long解法
    Jmeter命令行运行实例讲解
    Windows10在当前目录快速打开cmd的方法
    Jmeter接口测试对json串中的值进行断言
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/4755962.html
Copyright © 2020-2023  润新知