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
Object-C,文件路径API的更多相关文章
- C#常见的文件路径Api
我们经常有遇到要处理文件路径的需求,那么一般我们常见的有几种: 程序下面的文件 临时目录下的文件 获取程序下面的文件 首先我们创建了实例解决方案: 其中调用链是:Main.Shell->FooA ...
- Android用路径api在内部存储读写文件
复制并修改原有项目 复制之前创建的项目CC+CV操作 需要改动的地方: * 项目名字 * 应用包名 * R文件重新导包 接着修改件/AndroidManifest.xml中的包名:package=&q ...
- LoadLibrary文件路径及windows API相关的文件路径问题
LoadLibrary HMODULE WINAPI LoadLibrary( _In_ LPCTSTR lpFileName ); Loads the specified module into ...
- windows API实现用户选择文件路径的对话框
在编写应用程序时,有时需要用户选择某个文件,以供应用程序使用,比如在某些管理程序中需要打开某一个进程,这个时候需要弹出一个对话框来将文件路径以树形图的形式表示出来,以图形化的方式供用户选择文件路径,而 ...
- 01 语言基础+高级:1-5 常用API第二部分_day01.【Object类、常用API: Date类、System类、StringBuilder类】
day01[Object类.常用API] 主要内容 Object类 Date类 DateFormat类 Calendar类 System类 StringBuilder类 包装类 java.lang.O ...
- Spring配置文件详解 - applicationContext.xml文件路径
spring的配置文件applicationContext.xml的默认地址在WEB-INF下,只要在web.xml中加入代码 org.springframework.web.context.Cont ...
- Android根据文件路径加载指定文件
Android根据指定的文件路径加载指定文件格式(图片格式 png, gif,jpg jpeg)的文件相关信息的列表. 如图: 代码: public class Util { /**** * 计算文件 ...
- C#利用浏览按钮获得文件路径和文件夹路径
生成文件夹路径 private void btnChoose_Click(object sender, EventArgs e) { using (OpenFileDialog ...
- Spring配置文件详解 – applicationContext.xml文件路径
Spring配置文件详解 – applicationContext.xml文件路径 Java编程 spring的配置文件applicationContext.xml的默 ...
随机推荐
- Django(part4)
一个简单的form表单: #polls/templates/polls/detail.html<h1>{{ question.question_text }}</h1> {% ...
- Android 手势
GestureDetector:手势监听类,通常在View的setOnTouchListener方法中设置TouchListener,在TouchListener的onTouch函数中把MotionE ...
- 51nod 1717 好数 (水题)
题目: 看起来很复杂,其实就是有多少个素因子就翻转多少次. 然后考虑到只有平方数有奇数个素因子. 一次过,上代码把: #include <iostream> #include <al ...
- HttpWebRequest WebExcepton: The remote server returned an error: (407) Proxy Authentication Required.
1. Supply the credentials of the Currently Logged on User to the Proxy object similar to this: // Be ...
- 《剑指offer》数组中出现次数超过一半的数字
一.题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出 ...
- springmvc整合mybatis实现商品列表查询
转载.https://blog.csdn.net/chizhuyuyu/article/details/82180404 https://www.jianshu.com/p/689bdd11bfcc. ...
- requests 后续1
发送带数据post请求 import requests # 发送post请求 data = { } response = requests.post(url, data=data) # 内网 需要 认 ...
- layer弹窗
layer.alert(content, options, yes) - 普通信息框 它的弹出似乎显得有些高调,一般用于对用户造成比较强烈的关注,类似系统alert,但却比alert更灵便.它的参数是 ...
- webpack中optimization 的 runtimeChunk 是干嘛的
结论:把runtime部分的代码抽离出来单独打包 https://developers.google.com/web/fundamentals/performance/webpack/use-long ...
- luogu P1622 释放囚犯
题目描述 Caima王国中有一个奇怪的监狱,这个监狱一共有P个牢房,这些牢房一字排开,第i个紧挨着第i+1个(最后一个除外).现在正好牢房是满的. 上级下发了一个释放名单,要求每天释放名单上的一个人. ...