ios NSFileManager创建目录、文件
来源:互联网 发布:淘宝宝贝隐形降权 编辑:程序博客网 时间:2024/05/21 09:58
ios NSFileManager创建目录、文件
来源:http://www.cnblogs.com/wyqfighting/p/3182224.html
前言:接上篇博客,把对象写入文件,存入内存。那么,我们一般把需要保存的文件存放在什么位置了?沙盒中的Documetnts!
正文:先附上自己的代码
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *str1 = NSHomeDirectory();_filePath = [[NSString stringWithFormat:@"%@/Documents/imageViews/test.plist",str1]retain]; NSLog(@"%@",_filePath); if(![fileManager fileExistsAtPath:_filePath]){//如果不存在,则说明是第一次运行这个程序,那么建立这个文件夹 NSLog(@"first run"); NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSString *directryPath = [path stringByAppendingPathComponent:@"imageViews"]; [fileManager createDirectoryAtPath:directryPath withIntermediateDirectories:YES attributes:nil error:nil]; NSString *filePath = [directryPath stringByAppendingPathComponent:@"test.plist"]; NSLog(@"%@",filePath); [fileManager createFileAtPath:filePath contents:nil attributes:nil];......... }
此段代码创建的filePath为
一、创建~/Documents/imageViews/test.plist的详细步骤
1、找到Documetnts目录所在的位置
NSString *str1 = NSHomeDirectory();
str1为/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3
2、加上Documetnts这层目录
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
path为/Users/yuqiu/Library/Application Support/iPhone Simulator/6.1/Applications/2A8EFE9E-4CCA-41D1-8A5F-1FF00A115FA3/Documents
注意:使用的是stringByAppendingPathComponent:方法,不需要加“/"; 如果用的是别的方法加的话,需要写成@"/Documents".
3、在Documetnts目录中创建名为imageViews的目录
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *directryPath = [path stringByAppendingPathComponent:@"imageViews"];[fileManager createDirectoryAtPath:directryPath withIntermediateDirectories:YES attributes:nil error:nil];
创建NSFileManager的实例fileManager.NSFileManager这个类是专门进行文件管理的,可以创建文件,目录,删除,遍历目录等。我们调用了 createDirectoryAtPath:方法创建了一个新目录。
4、在imageViews目录里,创建文件test.plist.
NSString *filePath = [directryPath stringByAppendingPathComponent:@"test.plist"];[fileManager createFileAtPath:filePath contents:nil attributes:nil];
调用createFileAtPath:创建文件
最后得到的整个文件路径为:
二、NSFileManager常用的文件管理操作
1、创建目录 createDirectoryAtPath:
2、创建文件 createFileAtPath:
3、删除某个文件 removeItemAtPath:
4、检查某个文件是否存在 fileExistsAtPath:
5、检查文件是否可读 isReadableFileAtPath:
是否可写:isWritableFileAtPath:
6、取得文件属性 fileAttributesAtPath:
改变文件属性changeAttributesAtPath:
7、从path代表的文件中读取数据:contentsAtPath
8、移动文件movePath:toPath:handler:
9、复制文件copyPath:toPath:handler:
- ios NSFileManager创建目录、文件
- ios文件操作之目录操作-NSFileManager
- IOS管理文件和目录(NSFileManager用法)
- iOS—— NSFileManager ——管理文件和目录
- iOS文件系统---沙盒(sandbox),NSFileManager,NSFileHandle, NSData, 目录,文件
- iOS文件操作 NSFileManager
- iOS NSFileManager文件操作
- IOS-文件管理NSFileManager
- iOS NSFileManager(文件操作)
- ios读写文件NSFileManager
- ios 文件操作 NSFileManager
- iOS NSFileManager文件操作
- NSFileManager和文件的判断操作、获取目录、文件创建和目录创建。
- NSFileManager操作文件及目录
- NSFileManager管理文件和目录
- iOS文件处理,将图片存到手机目录里面 创建文件夹,NSFileManager用来存储图片,然后遍历将图片取出上传到服务器哦!!
- NSFileManager创建与拷贝文件
- Objective-C文件和目录操作,IOS文件操作,NSFileManager使用文件操作
- C++基类的指针
- 对MVC的初步理解
- VC 中一些常见的文件操作,包括获取当前路径、复制文件等
- 在windows下使用eclipse for c++来编译c/c++
- Linux 查看内核 系统 等软件版本命令
- ios NSFileManager创建目录、文件
- Zedboard之Hello World(ISE14.6)
- 中国风投70%利润来自互联网
- STL容器vector的内存问题
- Android网络编程实践之旅(二):基于Socket的网络通信
- Redis数据结构型数据库及其常用操作
- Hadoop --基础知识-合并文件简单示例
- Qt断点续传(暂停下载任务)
- 微软对Windows XP 的支持将在2014年4月8日结束