iOS中NSFileManager文件常用操作整合
//获取Document路径
+ (NSString *)getDocumentPath
{
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [filePaths objectAtIndex:];
} //获取Library路径
+ (NSString *)getLibraryPath
{
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
return [filePaths objectAtIndex:];
} //获取应用程序路径
+ (NSString *)getApplicationPath
{
return NSHomeDirectory();
} //获取Cache路径
+ (NSString *)getCachePath
{
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [filePaths objectAtIndex:];
} //获取Temp路径
+ (NSString *)getTempPath
{
return NSTemporaryDirectory();
} //判断文件是否存在于某个路径中
+ (BOOL)fileIsExistOfPath:(NSString *)filePath
{
BOOL flag = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
flag = YES;
} else {
flag = NO;
}
return flag;
} //从某个路径中移除文件
+ (BOOL)removeFileOfPath:(NSString *)filePath
{
BOOL flag = YES;
NSFileManager *fileManage = [NSFileManager defaultManager];
if ([fileManage fileExistsAtPath:filePath]) {
if (![fileManage removeItemAtPath:filePath error:nil]) {
flag = NO;
}
}
return flag;
} //从URL路径中移除文件
- (BOOL)removeFileOfURL:(NSURL *)fileURL
{
BOOL flag = YES;
NSFileManager *fileManage = [NSFileManager defaultManager];
if ([fileManage fileExistsAtPath:fileURL.path]) {
if (![fileManage removeItemAtURL:fileURL error:nil]) {
flag = NO;
}
}
return flag;
} //创建文件路径
+(BOOL)creatDirectoryWithPath:(NSString *)dirPath
{
BOOL ret = YES;
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:dirPath];
if (!isExist) {
NSError *error;
BOOL isSuccess = [[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
if (!isSuccess) {
ret = NO;
NSLog(@"creat Directory Failed. errorInfo:%@",error);
}
}
return ret;
} //创建文件
+ (BOOL)creatFileWithPath:(NSString *)filePath
{
BOOL isSuccess = YES;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL temp = [fileManager fileExistsAtPath:filePath];
if (temp) {
return YES;
}
NSError *error;
//stringByDeletingLastPathComponent:删除最后一个路径节点
NSString *dirPath = [filePath stringByDeletingLastPathComponent];
isSuccess = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"creat File Failed. errorInfo:%@",error);
}
if (!isSuccess) {
return isSuccess;
}
isSuccess = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
return isSuccess;
} //保存文件
+ (BOOL)saveFile:(NSString *)filePath withData:(NSData *)data
{
BOOL ret = YES;
ret = [self creatFileWithPath:filePath];
if (ret) {
ret = [data writeToFile:filePath atomically:YES];
if (!ret) {
NSLog(@"%s Failed",__FUNCTION__);
}
} else {
NSLog(@"%s Failed",__FUNCTION__);
}
return ret;
} //追加写文件
+ (BOOL)appendData:(NSData *)data withPath:(NSString *)path
{
BOOL result = [self creatFileWithPath:path];
if (result) {
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:path];
[handle seekToEndOfFile];
[handle writeData:data];
[handle synchronizeFile];
[handle closeFile];
return YES;
} else {
NSLog(@"%s Failed",__FUNCTION__);
return NO;
}
} //获取文件
+ (NSData *)getFileData:(NSString *)filePath
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *fileData = [handle readDataToEndOfFile];
[handle closeFile];
return fileData;
} //读取文件
+ (NSData *)getFileData:(NSString *)filePath startIndex:(long long)startIndex length:(NSInteger)length
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
[handle seekToFileOffset:startIndex];
NSData *data = [handle readDataOfLength:length];
[handle closeFile];
return data;
} //移动文件
+ (BOOL)moveFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath
{
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:fromPath]) {
NSLog(@"Error: fromPath Not Exist");
return NO;
}
if (![fileManager fileExistsAtPath:toPath]) {
NSLog(@"Error: toPath Not Exist");
return NO;
}
NSString *headerComponent = [toPath stringByDeletingLastPathComponent];
if ([self creatFileWithPath:headerComponent]) {
return [fileManager moveItemAtPath:fromPath toPath:toPath error:nil];
} else {
return NO;
}
} //拷贝文件
+(BOOL)copyFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath
{
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:fromPath]) {
NSLog(@"Error: fromPath Not Exist");
return NO;
}
if (![fileManager fileExistsAtPath:toPath]) {
NSLog(@"Error: toPath Not Exist");
return NO;
}
NSString *headerComponent = [toPath stringByDeletingLastPathComponent];
if ([self creatFileWithPath:headerComponent]) {
return [fileManager copyItemAtPath:fromPath toPath:toPath error:nil];
} else {
return NO;
}
} //获取文件夹下文件列表
+ (NSArray *)getFileListInFolderWithPath:(NSString *)path
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *fileList = [fileManager contentsOfDirectoryAtPath:path error:&error];
if (error) {
NSLog(@"getFileListInFolderWithPathFailed, errorInfo:%@",error);
}
return fileList;
} //获取文件大小
+ (long long)getFileSizeWithPath:(NSString *)path
{
unsigned long long fileLength = ;
NSNumber *fileSize;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
if ((fileSize = [fileAttributes objectForKey:NSFileSize])) {
fileLength = [fileSize unsignedLongLongValue];
}
return fileLength; // NSFileManager* manager =[NSFileManager defaultManager];
// if ([manager fileExistsAtPath:path]){
// return [[manager attributesOfItemAtPath:path error:nil] fileSize];
// }
// return 0;
} //获取文件创建时间
+ (NSString *)getFileCreatDateWithPath:(NSString *)path
{
NSString *date = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
date = [fileAttributes objectForKey:NSFileCreationDate];
return date;
} //获取文件所有者
+ (NSString *)getFileOwnerWithPath:(NSString *)path
{
NSString *fileOwner = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName];
return fileOwner;
} //获取文件更改日期
+ (NSString *)getFileChangeDateWithPath:(NSString *)path
{
NSString *date = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
date = [fileAttributes objectForKey:NSFileModificationDate];
return date;
}
iOS中NSFileManager文件常用操作整合的更多相关文章
- Hadoop HDFS文件常用操作及注意事项
Hadoop HDFS文件常用操作及注意事项 1.Copy a file from the local file system to HDFS The srcFile variable needs t ...
- go语言之进阶篇文件常用操作接口介绍和使用
一.文件常用操作接口介绍 1.创建文件 法1: 推荐用法 func Create(name string) (file *File, err Error) 根据提供的文件名创建新的文件,返回一个文件对 ...
- python 异常处理、文件常用操作
异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm
- OC NSFileManager(文件路径操作)
OC NSFileManager(文件路径操作) 初始化 NSFileManager * fm = [NSFileManager defaultManager]; 获取当前目录 [fm current ...
- Python基础灬文件常用操作
文件常用操作 文件内建函数和方法 open() :打开文件 read():输入 readline():输入一行 seek():文件内移动 write():输出 close():关闭文件 写文件writ ...
- iOS中几种常用的数据存储方式
自己稍微总结了一下下,方便大家查看 1.write直接写入文件的方法 永久保存在磁盘中,可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ...
- java中 File文件常用操作方法的汇总
一.IO流: 1.全称为:Input Output---------输入输出流. 输入:将文件读到内存中. 输出:将文件从内存中输出到其他地方. 2.IO技术的作用: 主要是解决设备与设备之间的数据传 ...
- linux下拷贝命令中的文件过滤操作记录
在日常的运维工作中,经常会涉及到在拷贝某个目录时要排查其中的某些文件.废话不多说,下面对这一需求的操作做一记录: linux系统中,假设要想将目录A中的文件复制到目录B中,并且复制时过滤掉源目录A中的 ...
- [转]Windows系统中监控文件复制操作的几种方式
1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得到源和目标文件名. 可以控制拒绝操作. 缺点: 不能对文件进行控制. 只对Shell文件操作有效, 对原生 ...
随机推荐
- 【转】关于gcc、glibc和binutils模块之间的关系
原文网址:http://www.mike.org.cn/articles/linux-about-gcc-glibc-and-binutils-the-relationship-between-mod ...
- video4linux(v4l)使用摄像头的实例基础教程与体会(转)
1. video4linux基础相关 1.1 v4l的介绍与一些基础知识的介绍 I.首先说明一下video4linux(v4l). 它是一些视频系统.视频软件.音频软 ...
- sed命令n,N,d,D,p,P,h,H,g,G,x解析
1.sed执行模板=sed '模式{命令1;命令2}'即逐行读入模式空间,执行命令,最后输出打印出来2.为方便下面,先说下p和P,p打印当前模式空间内容,追加到默认输出之后,P打印当前模式空间开端至\ ...
- Oracle数据库clob字段导出为sql insert插入语句
oracle数据库的clob字段导出为sql insert插入语句可以分三种情况:1,clob没有换行符:2,clob有换行符但不以分号结尾:3,clob有换行符并且以分号结尾. clob没有换行符使 ...
- Apache的下载安装(主要说的 64位)及问题
本文转载自:http://blog.csdn.net/qq_15096707/article/details/47319545 今天重装完win10系统,就重新下载安装 Apache.虽说之前有安装过 ...
- jmetr _MD5加密_获取签名
要达到的目的: app每个请求里面 请求头都带有一个 sign 的参数, 他的值是通过 开发自己设计的拼接方式 再通过md5加密生成 我们就是要生成这个sign的值出来 准备: 和开发要到签名组成公式 ...
- log4j示例-Daily方式(log4j.properties)
log_home=./log log4j.rootLogger=info log4j.category.com.ai.toptea.collection=Console,DailyFile,Daily ...
- 第三方登录之微信登录,基于ThinkSDK
本文基于ThinkSDK,为其补充微信登录demo 增加ThinkSDK的微信第三方登录 阅读本文之前请先了解ThinkSDK的文档 http://www.echomod.com/nexstep/fo ...
- hadoop Partiton中的字符串Hash函数改进
最近的MapReduce端的Partition根据map生成的Key来进行哈希,导致哈希出来的Reduce端处理任务数量非常不均匀,有些Reduce端处理的数据量非常小(几分钟就执行完成,而最后的pa ...
- 由 MySQL server 和 mysql-connector 版本的不匹配引发的一场惊魂
剧情还原 今天原计划给领导演示一个小Demo, 昨天在自己机器上调通OK以后就下班了... 今天上午早会后,领导说 “昨天,我让我们IT同事把新的测试环境搭建好了,XXX 你把要演示的Demo部署到上 ...