犀利吐槽
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 [email protected]"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