zoukankan      html  css  js  c++  java
  • Object-C,文件路径API

    犀利吐槽

    1.同样都是文件和目录操作",java中,就用java.util.File一个类,就封装了很多API,而Object-C搞了这么多类和函数。具体原因,有待分析啊。

    2.明明是NSString,字符串操作,怎么出现了”pathComponents“等操作文件路径相关的方法,很奇怪的赶脚。

    3.stringByAppendingString,这函数的名字有点长啊。

    4.总体感觉,Object-C的语法比Java复杂一些,码代码的效率低了不少。


    /
    /
    //  main.m
    //  FilePathUtil
    //
    //  Created by fansunion on 15/11/29.
    //  Copyright (c) 2015年 demo. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    
    //演示文件路径API
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
           NSString *fileName =@"path.m";
            NSFileManager *fm;
            NSString *path,*tempDir,*extention,*homeDir,*fullPath;
            NSArray *components;
            
            
            fm =[NSFileManager defaultManager];
            //临时目录
            tempDir = NSTemporaryDirectory();
            NSLog(@"The tempDir is %@",tempDir);
            
            //提取基本目录
            path =[fm currentDirectoryPath];
            NSLog(@"The base dir is %@",[path lastPathComponent]);
            
            //fileName在当前目录中的完整路径
            //这个地方有个问题
            //本地输出”/Users/fansunion/Library/Developer/Xcode/DerivedData/FilePathUtil-bvzjqehotbexooebruphtwcmqekz/Build/Products/Debugpath.m“
            //Debug和path.m之间没有”分隔符“/",而书本中的例子是有的
            //最好还是手动加上,Java中也是没有这个分隔符,需要手动加上的
            fullPath =[path stringByAppendingString:fileName];
            NSLog(@"The fullPath is %@",fullPath);
            
            //获得文件扩展名
            extention = [fullPath pathExtension];
            NSLog(@"The extentions is %@",extention);
            
            //获得用户的主目录
            homeDir = NSHomeDirectory();
            NSLog(@"The home directory is %@",homeDir);
    
            //拆分路径为各个组成部分
            components = [homeDir pathComponents];
            for(path in components){
                NSLog(@"%@",path);
            }
        }
        return 0;
    }

    程序输出

    2015-11-29 13:43:30.550 FilePathUtil[2861:179163] The tempDir is /var/folders/4q/5ylpds9n5n97bq_r41qvly4w0000gn/T/

    2015-11-29 13:43:30.551 FilePathUtil[2861:179163] The base dir is Debug

    2015-11-29 13:43:30.551 FilePathUtil[2861:179163] The fullPath is /Users/fansunion/Library/Developer/Xcode/DerivedData/FilePathUtil-bvzjqehotbexooebruphtwcmqekz/Build/Products/Debugpath.m

    2015-11-29 13:43:30.551 FilePathUtil[2861:179163] The extentions is m

    2015-11-29 13:43:30.552 FilePathUtil[2861:179163] The home directory is /Users/fansunion

    2015-11-29 13:43:30.552 FilePathUtil[2861:179163] /

    2015-11-29 13:43:30.552 FilePathUtil[2861:179163] Users

    2015-11-29 13:43:30.553 FilePathUtil[2861:179163] fansunion

    Program ended with exit code: 0


  • 相关阅读:
    cmd运行jar包,生成随机密码
    前端实时搜索框模拟
    JS日期格式化
    批量修改行尾注释(代码规范检查中)
    Java操作某方法时报错:java.lang.NoSuchMethodError
    正则匹配数字
    [转]SSH框架简介
    Java根据地理位置获取经纬度(调用百度地图API)
    [转]DevOps究竟是什么鬼?
    MyEclipse的html页面 design视图中 关闭可视化界面
  • 原文地址:https://www.cnblogs.com/qitian1/p/6462565.html
Copyright © 2011-2022 走看看