iOS中NSFileManager的使用
来源:互联网 发布:淘宝优惠券赚钱 编辑:程序博客网 时间:2024/06/01 08:43
//NSFileManager用于判断
NSString * filePath=@"/Users/ms/Desktop/contact.json";
NSString * filePath1=@"/Users/ms/Desktop";
//1.文件是否存在
NSFileManager * fm=[NSFileManagerdefaultManager];
//调用defaultManager,创建一个文件管理的单例对象
BOOL isYES= [fm fileExistsAtPath:filePath];
NSLog(@"%d",isYES);
//2.文件是否是一个目录
BOOL isDir;
[fm fileExistsAtPath:filePath1 isDirectory:&isDir];
if (isDir) {
NSLog(@"这是一个目录");
}else
{
NSLog(@"这不是一个目录");
}
//3.文件是否可读
isYES=[fm isReadableFileAtPath:filePath];
NSLog(@"%d",isYES);
//4.文件是否可写
isYES=[fm isWritableFileAtPath:filePath];
NSLog(@"%d",isYES);
//5.文件是否可删除
isYES=[fm isDeletableFileAtPath:filePath];
NSLog(@"%d",isYES);
===============================================================
//创建文件对象
NSFileManager * fm=[NSFileManagerdefaultManager];
NSString * path=@"/Users/ms/Desktop/contact.json";
NSString * path1=@"/Users/ms/Desktop";
//1如何获取文件属性
NSDictionary * dict=[fmattributesOfItemAtPath:path error:nil];
NSLog(@"%@",dict);
NSLog(@"%@",dict[@"NSFileType"]);
//2获取指定目录下的子目录
//使用递归的方式获取当前目录及子目录下的所有的文件和文件夹
NSArray * subPath= [fm subpathsAtPath:path1];
// NSLog(@"subPath=%@",subPath);
subPath=[fm subpathsOfDirectoryAtPath:path1error:nil];
//这种方式不用递归的方式获取,递归压栈,费时间和内存
//NSLog(@"subPath=%@",subPath);
//3获取指定目录下的文件及文件夹
subPath= [fm contentsOfDirectoryAtPath:path1error:nil];
NSLog(@"subPath=%@",subPath);
}
===========================================================
//创建文件管理对象
NSFileManager * fm=[NSFileManagerdefaultManager];
//如何创建目录(/Users/ms/Desktop/aaa)
NSString * createPath=@"/Users/ms/Desktop/aaa";
// fm createDirectoryAtPath:@"路" withIntermediateDirectories:@"如果aaa前面没有desktop是否创建aaa文件前面文件" attributes:@"属性" error:@"错误对象"
BOOL isYES=[fmcreateDirectoryAtPath:createPath withIntermediateDirectories:NOattributes:nilerror:nil];
if (isYES) {
NSLog(@"成功");
}
//如何创建文件
NSString * createPath=@"/Users/ms/Desktop/aaa/love.txt";
NSString * str=@"每当我错过一个女孩,我就向山上放一块砖,于是就有了长城";
// fm createFileAtPath:@"路径" contents:NSdata类型的数据 attributes:文件的属性的字典
//创建NSdata用于处理二进制数据的类
NSData * data=[strdataUsingEncoding:NSUTF8StringEncoding];
BOOL isYES=[fm createFileAtPath:createPath contents:data attributes:nil];
if (isYES) {
NSLog(@"%d",isYES);
}
//如何copy文件
NSString * createPath=@"/Users/ms/Desktop/aaa/love.txt";
NSString * createPath1=@"/Users/ms/Desktop/love.txt";
//[fm copyItemAtPath:createPath toPath:createPath1 error:nil];
//如何移动文件
[fm moveItemAtPath:createPath toPath:createPath1 error:nil];
//如何删除文件
[fm removeItemAtPath:createPath1 error:nil];
- iOS中NSFileManager的使用
- IOS中NSFileManager的操作
- ios开发,NSFileManager的使用
- iOS开发12-iOS NSFileManager的使用
- IOS中NSFileManager的操作 [Z]
- iOS中NSFileManager的简单用法
- iOS开发笔记之NSFileManager的使用
- iOS开发 NSFileManager的基本使用
- ios NSFileManager的细节
- NSFileManager的使用
- NSFileManager的使用
- NSFileManager的使用
- NSFileManager的使用
- IOS中NSFileManager的操作 [Z](文件操作)
- iOS NSFileManager文件系统的管理
- iOS NSFileManager文件系统的管理
- iOS NSFileManager文件系统的管理
- iOS - NSFileManager的读写操作
- iOS中NSnumber的使用 NSValue的使用
- js日期时间函数
- Android 移动开发知识体系
- Runtime之分类与协议
- SQL第二课
- iOS中NSFileManager的使用
- Elasticsearch 并发修改乐观锁
- AWS上创建AMI Image以及mount新的卷
- C++中回调函数的一个简单例子?
- Java并发之volatile
- JVM学习03-类加载机制
- 提高android源码 编译速度参考
- poll()函数详解
- jni 生成不同芯片对应的so文件